I have a problem when using the trim() function in php. As shown above, the output of the codes is empty string if the input argument if NULL. Is there any way to avoid this? It seems the trim will return empty string by default if the input is unset or NULL value. This give me a hard time to
Tag: php
Finding if a number is a power of 2
Just out of curiosity, how can you tell if a number x is a power of two (x = 2^n) without using recursion. Thanks Answer One way is to use bitwise AND. If a number $x is a power of two (e.g., 8=1000), it will have no bits in common with its predecessor (7=0111). So you can write: Note: This
Remove non-numeric characters (except periods and commas) from a string
If I have the following values: $var1 = AR3,373.31 $var2 = 12.322,11T How can I create a new variable and set it to a copy of the data that has any non-numeric characters removed, with the …
PHP – Get bool to echo false when false
The following code doesn’t print out anything: But the following code prints 1: Is there a better way to print 0 or false when $bool_val is false than adding an if statement? Answer Or if you only want output when it’s false:
PHP Undefined Constant PHP_ROUND_HALF_DOWN
I have some PHP code in a project I’m working on that uses PHP’s round function. On my localhost, I don’t include any quotes around my mode argument, stating it as just PHP_ROUND_HALF_DOWN. However, when pushing to my server I get the error message: Now, when I add the single quotes to the m…
Behat over Cucumber in PHP
Don’t get me wrong. I think both projects are fantastic. But as both php and ruby developer I wonder if there are there any compelling reasons, besides possible language barrier, why would one choose …
Does PHP support the RAII pattern? How?
Most resources on PHP never touch memory management because the language itself is pretty good at doing this for you. However, in PHP you often end up dealing with external resources which aren’t memory — database handles, sessions, database transactions, etc. These external resources could be man…
PHP session expiring too early
I have an issue, my apps sessions are expiring unexpectedly after about 15 minutes. I need them to expire after 4 hours. The server is Centos 5.5, PHP is 5.3.2. Below is my code (included in an global header). My .htaccess file contains: Any help or insight would be greatful. EDIT: I was unable to login when …
Location for session files in Apache/PHP
What is the default location of session files on an installation of Apache/PHP on Ubuntu 10.10?
Problem return a function inside of a shortcode in WordPress
I have some trouble coding a themes shortcode. I want the code to display a div with a view counter function, and then a link with the shotcodes content as the url. The view_count(); function works fine when called inside theme files, and I actually managed to make it show, but then it displayed before the_co…