docs:programming:php:php_notes

php notes

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

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.”

  • php_value
    • For setting normal strings and values
  • php_flag
    • For setting boolean values
  • php_admin_value
    • For setting administrative values
  • php_admin_flag
    • For setting boolean administrative values

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

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 is a variable which represents the currently executing script (the name of the file you are viewing/running).

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

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 — Assign variables as if they were an array:

<?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";
 
?> 
<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>

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 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.

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.

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>

  • use <html>&#061;&gt;</html> with the foreach command on associative arrays
    • foreach($array as $key<html>&#061;&gt;</html>$value)…
    • to assign values to associative arrays (inside the array function), use <html>&#061;&gt;</html>:
      $foo = array(
      	"fname" => "John",
      	"lname" => "Doe",
      	"planet" => "Earth"
      );
    • to assign values to associative arrays (line by line), use the = sign
      • $array[item1] = “orange”
      • $array[item2] = “banana”
  • use <html>&#045;&gt;</html> for class member references
    • $this<html>&#045;&gt;</html>functionName()
    • $this<html>&#045;&gt;</html>variableName
// 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
// 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
  • to use heredoc, the file needs to be in unix format, not Macintosh because of line ending issues
  • the EOD_HEREDOC identifier below can actually be anything you want, but it must end with nothing else on the line
$foo = <<< EOD_HEREDOC
  This is a "Multiline" string, without the need
  to escape quotes, assigned using the heredoc syntax
EOD_HEREDOC;
  • if you add a normal function in a file after a class has been defined, and you want to access the members of the class within the normal function, you need to declare the class as “global” first
  • if you use require or include, make sure to use a relative or full path to avoid duplicating a filename already in the php include path (such as DB.php)
  • docs/programming/php/php_notes.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1