Skip to content
Advertisement

Replace string in an array with PHP

How can I replace a sub string with some other string for all items of an array in PHP? I don’t want to use a loop to do it. Is there a predefined function in PHP that does exactly that? How can I do that on keys of array? Answer But array_map is just a hidden loop. Why not use

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

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 mode argument, the first error goes away,

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 managed most cleanly using some form of RAII object. I initially thought that PHP

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 i updated my

Advertisement