[PHP]memory limit

 

Encountered problem :

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

 

Tempory solution for debug :

Add ini_set(‘memory_limit’, ‘-1’);  in for example index.php to turn off the default memory control of PHP.

This is not the best solution, because it means that the program can use as much memory as it wants, maybe that will cause a memory overflow. The best way is to improve the program, for example pagnation to a batch etc.

[PHP] PHP code showing on the page instead of being executed

Environment :
Ubuntu Server 14.04
PHP 5.5.9

Problem :
PHP code showing on the page instead of being executed

Reason :
The reason that I met this problem is because the PHP file I used begin with (<?) instead of (<?php) which the short tag is deprecated by default.

Solution :
Find php.ini in /etc/php5/apache2, make sure the option short_open_tag=On, if not, change if to On.

Another solution more complete from stack overflow : http://stackoverflow.com/a/5121589/740546

[OpenShift] Deploy PHP/MySQL in OpenShift got 503 Service Temporarily Unavailable / 504 Gateway Time-out

Context :

The objective is to deploy the connection of MySQL with PHP in an OpenShift Server.
OpenShift is PaaS provided by Red Hat. For more info, read wikipedia OpenShift.

Problem :

The server returns :

503 Service Temporarily Unavailable

or

504 Gateway Time-out

Continue reading

[Install PHPUnit] Installing PEAR and PHPUnit on WAMP and Windows 7 (no longer maintained from PHPUnit 3.7)

First of all, thanks to @Sebastian Bergmann for reminding and sorry for my careless about this reblog because End of Life for PEAR Installation Method to PHPUnit from github and The package PHPUnit is not maintained anymore and has been superseded. Package has moved to channel pear.phpunit.de, package PHPUnit from pear.php.net.

So this blog should be a reblog of installing PHPUnit(<3.7) in Windows7 from http://thecancerus.com/ and I found it was still useful for understanding the theory of configuration of php(in wamp) and the environment variables.

The newest manual installation of PHPUnit in windows can be found its documentation.

Continue reading

[PHP] upload file and zip with password

Background (needs):
1. upload a file without checking the type (whatever the type)
2. move the file into a specific folder
3. zip the file with password

Thanks to the function move_uploaded_file, it is very simple to move file to wherever you want, of course, you must have the privilege to do that.

As it is quite simple, I put the source code directely here :
Continue reading

[PHP] PHP Warning: Module ‘mcrypt’ already loaded in Unknown on line 0

Like in the title, PHP Warning: Module ‘mcrypt’ already loaded in Unknown on line 0.
When we launch whatever a command PHP, for example php -v, we will see the warning :

# php -v
PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/mcrypt.ini on line 1 in Unknown on line 0
PHP Warning:  Module 'mcrypt' already loaded in Unknown on line 0
...

This warning was caused by the duplicated configuration of the mcrypt extension.

To check it, we can use this command (the path could be changed depends on the local configuration):

grep "extension=mcrypt.so" /etc/php5/cli/php.ini

and

grep "extension=mcrypt.so" /etc/php5/cli/conf.d/mcrypt.ini

If the two command return the same thing : “extension=mcrypt.so”, this means the extension is included two times.

The solution is simple, juste remove one of these configuration, that will be ok.

[PHP] PHP Fatal error can’t use method return value in write context

Context :
Development with PHP

Error message :
PHP Fatal error can’t use method return value in write context

Code generated error :

if(empty(preg_match($pattern, $subject))){
  ....
}

Error reason (from php.net) :

Note:
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

Code corrected :

preg_match($pattern, $subject, $match);
if(empty( $match )){
  ....
}

[PHPUnit] Testing output

Test contains not only unit test, integration test, but also the output of screen, including files etc.

For example, with the function exceptOutputString, we can test the string output by echo or print.

<?php
class OutputTest extends PHPUnit_Framework_TestCase
{
    public function testExpectFooActualFoo()
    {
        $this->outputString();    // Call my local function
        $this->expectOutputString('Hello world!');    // Comparaison
    }

    // local function 
    private function outputString()
    {
        echo "Hello world!";
    }
}
?>

Ref. Methods for output testing

[PHPUnit] Failed opening required autoload.php error

After a re-installation of the operating system Ubuntu 12.04, I got a problem by installing/using PHPUnit, this maybe not the same as your case, but I posted it just for sharing, I hope that could help.

Before all, I just want to remind that Using the PEAR Installer to install PHPUnit is no longer supported, more info in pear.phpunit.de and github.
Continue reading