Skip navigation

Tag Archives: php

Darn!, this is nothing but a little note of caution, a sad admission of stupidity or maybe even a tip to someone: if you ever find yourself in a tight schedule doing extra hours to finish up a project, don’t waste precious minutes debugging your app only due to having foolishly picked the php ISO-8601 numeric representation of the day of the week “N” (mon=0 … sun=7). Completing the missing dates from a set of database records will work flawlessly just with date(“w”), specially if you had pulled the existing days using mysql date_format(“w”).

here’s an example of how to run php in the command line, something I almost never do and would like to remember by writing it down.
Simply echo the php statement (within its corresponding tags) and pipe it to the php service. Eg:

echo ‘<?php $str = ‘2144338’; echo substr($str,0,2); ?>’ | php

or even more usefully:

echo ‘<?php¬† phpinfo(); ?>’ | php

The passhtru command in php permits to get the outcome of a unix shell command. If you never knew or heard about it, take a look at it in the manual. It is a fantastic mean to use any shell, perl o unix command from within the php language itself as this allows to output the result of a command in a close fashion to what system() or exec() also do.
In my particular case I ran into a situation hard to debug in which a script was failing only when requested inside another, with either a require or include call. The script failed with a fatal error of memory exhaustion. I didn’t have much time to study and re-code the script to avoid the problem, but it may be worth mentioning the alternative use that helped me to circumvent the failure when I simply used passthru to grab the output of the php script and then print it out inside the function in the way I wanted.

Here’s what I pretty much did:

passthru("php  tree.php --section ".$section." | sed -e '1,8 d'", $notOk);
$tree_outcome = ob_get_contents();

 echo '<div id="siteTree">',$tree_outcome,'</div>';

See also that the output is being piped to a sed command only to filter out the first 8 lines that I didn’t use.
As an extra note I had to slightly modify the tree.php script to process the shell argument “section” being passed which now needed to be read inside the $_SERVER[‘argv’] variable.

I don’t know of any drawback or potential problems of this method, it’s a workaround that I was happy to find, so comments from php coders will be very welcome.