Archive

Archive for the ‘PHP’ Category

Override php.ini to turn on error reporting, and display errors

February 13th, 2009 1 comment

If you’re server doesn’t allow you to show error in php try the following lines at the near the top of your script.

Try this:

<?php
// Report all PHP errors
error_reporting(E_ALL);
ini_set("display_errors", 1);

If it still doesn’t show up, the web server daemon may not have write permissions to the default logging directory.

Categories: PHP Tags: , ,

Wrapping CDATA

December 26th, 2008 No comments

function clean_up_xml($xml_string='', $array_of_tags)
{
    if (count($array_of_tags) == 0)
    {
        throw new Exception('Second parameter, must be an array of strings');
    }
    foreach ($array_of_tags as $tag)
    {
        // Clean up tags, and trim whitespaces around the tag.
        $needle[]  = "@[\n\r\t ]*(\<[/]?{$tag}\>)[\n\r\t ]*@";
        $replace[]  = '$1';
        // find tags that don't have CDATA, and wrap them with CDATA
        $needle[]  = "@\<{$tag}\>(?!\<\!\[CDATA\[)(.*?)\</{$tag}\>@ms";
        $replace[]  = "<{$tag}><![CDATA[$1]]></{$tag}>";

        $xml_string = preg_replace($needle, $replace, $xml_string);
    }
    return $xml_string;
}

Explanation:

  1. Remove all spaces, tabs, \r, and \n characters around the desired tag, replace with just the tag itself
  2. Find all occurances of $tag, but not followed by <![CDATA[, and wrap contents in <![CDATA[ ... ]]>.
Categories: PHP Tags:

Poor logic

November 21st, 2008 No comments

Golden tidbit I found while doing some maintenance:

$count = count($some_array);

if($count)
{
    if($count > 1)
    {
        $val = 'PluralText';
    }
    elseif ($count == 1)
    {
        $val = 'SingularText';
    }
    elseif ($language == 'fr')
    {
        $val = 'PluralText';
    }
    else
    {
        $val = 'SingularText';
    }
}
else
{
   $val = 'None found';
}

could have been just written as

if ( $count = count( $some_array ) )
{
    $val = ( $count > 1 ) ? 'PluralText' : 'SingularText';
}
else
{
    $val = 'None found';
}

The reason in this example, the first section language code would never be reached.

The program would never reach the “$language” if condition. Allowing us to remove that extra code.

Simplifying the code to the point where it’s easily readable. Some may say that ternary statements are cryptic to read. I agree for the most poart. But when setting a simple value… say setting up initial values.

$value =
            (empty($this->getRequestParameter('some_random_parameter'))
       ?
            null
       :
            $this->getRequestParameter('some_random_parameter');

is better then writing

if (!empty($this->getRequestParameter('some_random_parameter'))
{
    $value = $this->getRequestParameter('some_random_parameter');
}
else
{
    $value = null;
}
Categories: PHP Tags:

Why didn’t the programmer run their program?

November 21st, 2008 No comments

The error is simply this

mysql_quety(...);

If the programmer originally check their code by just running the script, the php parser would have immediately stated that it does not know the function “mysql_quety()”.  I’m surprised that this passed the programmers initial inspection.

This leads to the question in the title…

Categories: PHP Tags: ,

Our first tidbit was found by Sean!

November 21st, 2008 No comments

in our code…

<?php

$var = doQuery($something);

however, “doQuery” was never defined.  The original programmer never even ran the program after writing it.

Moral of the story, test your code at least by running it.

Categories: PHP Tags: ,