====== 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." * **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. ===== .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: Order allow,deny Deny from all ===== $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: * (PHP4) http://us3.php.net/manual/en/language.oop.php * (PHP5) http://us3.php.net/manual/en/language.oop5.php ===== list() ===== list — Assign variables as if they were an array: ==== Example 298 ==== ==== Example 299 ==== \n" . " \n" . " \n" . " \n"; } ?>
Employee name Salary
$name$salary
===== 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. ===== print_r() and var_dump() ===== 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. ===== => and -> symbols ===== * use => with the foreach command on associative arrays * foreach($array as $key=>$value)... * to assign values to associative arrays (inside the array function), use =>: $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 -> for class member references * $this->functionName() * $this->variableName ===== 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 ===== * 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; ===== accessing class members from another function ===== * 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 ===== require/include ===== * 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)