[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

[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 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 )){
  ....
}

[PHP] gmdate function date limit in 32 bits system

Today I got this problem when executing the gmdate in PHP.

echo gmdate('Y-m-d\T00:00:00Z', strtotime("9999-12-31T00:00:00Z"));

While normally it should return “9999-12-31T00:00:00Z”, but which made me surprised is that finally I got a “1970-01-01T00:00:00Z”.
And I tried this command in another PC, which returned me the good answer.

Where did the 1970 came from? I guessed this might be something like the start of the world or the creation of PHP or etc.

Bon, ok ! Let’s check out the difference between the version of the system and the version of PHP.

The first PC (Ubuntu)which returned me a “1970” got this :

# uname -a
Linux 2.6.32-38-generic #83-Ubuntu SMP Wed Jan 4 11:13:04 UTC 2012 i686 GNU/Linux

The second PC which returned me the “9999” got this :

#uname -a
Linux 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

I think I got the answer, if it is a 32-bit unix-like system, the time stamp is based in seconds, and the biggest by calculating 2^32 something like that will be a number 2147483647, so nearly 68 years, and the begin year for unix time (defined by IEEE ?) is 1970, so the max is 1970 + 68 = 2038. So here if the year is bigger than 2038, there will be an integer overflow, an error of course, all… will return back to the beginning 🙂

Find more explanation in wiki :
http://en.wikipedia.org/wiki/Year_2038_problem

Find an answer in stack overflow :
http://stackoverflow.com/a/5879202

[PHP] Compare string to 0 in PHP

The question seems simple and naif but if you really do it, it’s a little annoying.

You may going to met this kind of case :

if('your_string' == 0) {
 echo "The result of 'your_string' == 0 is Equivalent\n";
} else {
 echo "The result of 'your_string' == 0 is Not Equivalent\n";
}

if('your_string' == 1) {
 echo "The result of 'your_string' == 1 is Equivalent\n";
} else {
 echo "The result of 'your_string' == 1 is Not Equivalent\n";
}

if('your_string' == '0') {
 echo "The result of 'your_string' == '0' is Equivalent\n";
} else {
 echo "The result of 'your_string' == '0' is Not Equivalent\n";
}

if('your_string' == '1') {
 echo "The result of 'your_string' == '1' is Equivalent\n";
} else {
 echo "The result of 'your_string' == '1' is Not Equivalent\n";
}

In my post, the result is :
The result of ‘your_string’ == 0 is Equivalent
The result of ‘your_string’ == 1 is Not Equivalent
The result of ‘your_string’ == ‘0’ is Not Equivalent
The result of ‘your_string’ == ‘1’ is Not Equivalent

So you see here, the first check is true, that we don’t want.
To avoid the problem, what you have to do is compare with the type, like :

if("your_string" === 0) ...

if( strval(</span>"your_string") === 0) ...

[Apache] Install configuration perl and php for apache

To install the configuration of perl and php for apache in Ubuntu, it is sufficient to execute several commands.

For perl : sudo apt-get install apache2

For php : sudo apt-get install php5 libapache2-mod-php5

After doing these commands, if we check whether the modules are avaliable, we just need a short script :

check_mod(){
    mode=$1
    mods_enabled=/etc/apache2/mods-enabled
    mods_available=/etc/apache2/mods-available

    if [ -f "$mods_enabled/$mode" ] ; then
    echo "The module $mode is enabled"
    else  
    if [ -f "$mods_avalable/$mode" ] ; then
        echo "The module $mode is available"
        sudo ln -s $mods_available/$mode $mods_enabled/$mode
    else  
        echo "[KO]The module is not installed[KO]"
    fi
    fi
}

check_mod perl.load
check_mod php5.conf
check_mod php5.load

Ref.
https://help.ubuntu.com/10.04/serverguide/php5.html
http://www.blog.highub.com/perl/install-configure-apache-localhost-perl-on-linux-ubuntu/

[PHP/DOM]remove a XML node itself

We can remove a child node in Dom by php (example from php.nex):

$doc = new DOMDocument;
$doc->load('book.xml');

$book = $doc->documentElement;

// we retrieve the chapter and remove it from the book
$chapter = $book->getElementsByTagName('chapter')->item(0);
$oldchapter = $book->removeChild($chapter);

echo $doc->saveXML();

But if you want to delete the node itself, you could do something like this, it will delete the first appearance of the node:

$dom = new DomDocument();
$doc = $dom->loadXML($strXml);
$tagName = $dom->getElementsByTagName('tagName')->item(0);
$tagName->parentNode->removeChild($tagName);
echo $dom->saveXML();