Table of Contents

php notes

This page is for things that I commonly forget regarding php.

httpd.conf / .htaccess configuration options

Any options can be set in httpd.conf, but you can only set options in an individual .htaccess file if the Apache AllowOverrides includes “Options.”

See http://www.php.net/configuration for more information.

.inc files

Instead of using .php as your extension for files to be included, use something like .inc. If you use .php, then the include files can be executed by the user, which probably isn't as you intended. You can block direct access to .inc files by the following lines in an .htaccess file:

<FILES ~ "\.inc$">
  Order allow,deny
  Deny from all
</FILES>

$PHP_SELF

$PHP_SELF is a variable which represents the currently executing script (the name of the file you are viewing/running).

Dynamic Variables

You don't always have to know the variable name beforehand.

	$var = "hello"; // $var now has a value of "hello"
	$$var = "hello there";  // new variable $hello has a value of "hello there"

There is a special syntax for using dynamic variables, and any other complex variable, inside quoted strings in php. This syntax also prevents ambiguity with variable arrays:

	echo "Hello ${$var}";  // string example

	echo ${$var[1]};  // array example

Objects

PHP 4 has limited support for objects. PHP 5 is supposed to improve the object-oriented capabilities of PHP.

class test{
  var $str = "Hello World";
  function init($str){
    this->str = $str;
  }
}

$class = new test;
echo $class->str;
$class->init("Hello");
echo $class->str;

Inheritance:

class more extends test{
  function more(){             // if a function has the same name as the class,
                               // then the function will be called as soon
                               // as the class is instantiated
    echo "Constructor called";
  }
}

More information is available at:

list()

list — Assign variables as if they were an array:

Example 298

<?php
 
$info = array('coffee', 'brown', 'caffeine');
 
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
 
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
 
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
 
?> 

Example 299

<table>
 <tr>
  <th>Employee name</th>
  <th>Salary</th>
 </tr>
 
<?php
 
$result = mysql_query("SELECT id, name, salary FROM employees", $conn);
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
   echo " <tr>\n" .
         "  <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
         "  <td>$salary</td>\n" .
         " </tr>\n";
}
 
?>
 
</table>

break n

You can use break to stop a loop (if, while, for, etc…). I usually forget that you can break out of nested loops of many levels to the point you want by following break with a number.

continue n

Continue can be used to skip the rest of the current loop and continue with the next iteration of a loop. As with break, you can follow continue with a number of how many loops you want to skip out of.

These functions print the type and value of an expression in a human-readable form. These functions are useful for debugging a script, especially when dealing with arrays or objects.

echo vs. print

The echo statement and the print function are identical except that the print function has a return value. This makes the print function suitable for complex expressions and conditional operations.

end[ if | switch | while | for ]

Instead of always doing loops like this:

if(expr){
	// do something
}else{
	// do something else
}

…you can write them like this:

if(expr):
	// do something
else:
	// do something else
endif;

It may be easier to write them using the second method if you have intermixed code and HTML. </code>

=> and -> symbols

testing for variable existence

// note - a variable could be set, but set with no value
if(isset($var))
  // do something
 
// is the variable set and not empty
if(!empty($var))
  // do something
 
// using the variable name as a test is the same as !empty
if($var)
  // do something

testing for string equality

// use this when testing for simple string equality
if($var == "match string")
  // do something

// three equals (===) check for equality of value and type
// i.e. 0.0 == 0 (true)
// i.e. 0.0 === 0 (false)
if($var === "match string")
  // do something

// strcasecmp and strcmp will return higher, lower or zero depending
// if the match string is considered higher, lower or equal
if(strcasecmp($var, "match string")==0)
  // do something

heredoc syntax

$foo = <<< EOD_HEREDOC
  This is a "Multiline" string, without the need
  to escape quotes, assigned using the heredoc syntax
EOD_HEREDOC;

accessing class members from another function

require/include