.Open PHP Samples This page was created by Paul Conrad as part of his Graduate Independent Study (CSCI695) of the syntax and semantics of the PHP language. This page contains all of the code samples for PHP. . See Also For a quick, concise reference to terms used through out this page, see .See ./php.glossary.html which contains meta-linguistic terms, lexemes, and keyword definitions based on the C++ glossary found at .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html with the appropriate adaptations to PHP. . PHP comments .As_is To see this script in action, visit .See ./php_samples/comment_sample.php . . PHP variables .As_is To see this script in action, visit .See ./php_samples/variable_sample.php (script does not include the two lines that will fail). . PHP functions .As_is "; .As_is ?> To see this script in action, visit .See ./php_samples/function_sample1.php . The following script is an example of case-insensitivity with function names in PHP. To see this script in action, visit .See ./php_samples/function_sample2.php . .As_is "; .As_is } .As_is foo("foo is foo"); .As_is Foo("Foo is foo"); .As_is FoO("FoO is foo"); .As_is FOO("FOO is foo"); .As_is ?> The two following sample scripts show two different ways of using the same anonymous function. .As_is To see this script in action, visit .See ./php_samples/anonymous_function.php . .As_is To see this script in action, visit .See ./php_samples/anonymous_function2.php . PHP Arrays Arrays are associative and can use strings(in quotes) or integers. .Hole PHP_Arrays . Getting data from an HTML form Data sent from an HTML form is presented to a PHP script as an array. It may be the array $_GET or the array of $_POST. Before you access an element in one of these arrays you need to check to see it exists. Recall that any PHP script can be called by typing inits URL... and so any or no data can be delivered. The following function handles this. .As_is function get($key) .As_is { .As_is if (array_key_exists($key,$_POST)) .As_is return $_POST[$key]; .As_is else .As_is return ""; .As_is } .As_is $SUBJECT=get('subject'); .As_is $FROM=get('from'); .As_is $MESSAGE=get('message'); .As_is $SIGNED=get('signed'); . PHP classes .As_is name=$name; .As_is $this->experience=$experience; .As_is $this->lang=$lang; .As_is $this->education=$education; .As_is } .As_is // Getter/Setter functions for all properties in the class .As_is function get_name() { .As_is return $this->name; .As_is } .As_is function set_name($newname) { .As_is $this->name = $newname; .As_is } .As_is function get_experience() { .As_is return $this->experience; .As_is } .As_is function set_experience($newexperience) { .As_is $this->experience = $newexperience; .As_is } .As_is function get_lang() { .As_is return $this->lang; .As_is } .As_is function set_lang($newlang) { .As_is $this->lang = $newlang; .As_is } .As_is function get_education() { .As_is return $this->education; .As_is } .As_is function set_education($neweducation) { .As_is $this->education = $neweducation; .As_is } .As_is // Utility data dump function .As_is function output() { .As_is echo "Programmer Name: ".$this->name."
"; .As_is echo $this->name." has ".$this->experience." years of programming experience.
"; .As_is echo $this->lang." is ".$this->name."'s favorite programming language.
"; .As_is echo $this->name." holds the degree: ".$this->education."

"; .As_is } .As_is } .As_is // Instantiating a programmer .As_is $paul = new Programmer('Paul Conrad',12,'C++','Bachelor of Science in Computer Science'); .As_is $paul->output(); .As_is // Oops, Paul has programmed alot longer than 12 year, really is 22 years .As_is $paul->set_experience(22); .As_is $paul->output(); .As_is ?> To see this script in action, visit .See ./php_samples/class_sample.php . . PHP constants .As_is "; .As_is echo "Our school's Computer Science Dept has ".programs." academic programs"; .As_is ?> To see this script in action, visit .See ./php_samples/constant_sample.php . . PHP Sample Code Quick Reference .Table Example PHP Script Source File Description .Row .See ./php_samples/anonymous_function.php .Item .See ./php_samples/anonymous_function.txt .Item Sample of anonymous functions at work. .Row .See ./php_samples/anonymous_function2.php .Item .See ./php_samples/anonymous_function2.txt .Item Another anonymous function sample, no assigned variable, though (YAGNI). .Row .See ./php_samples/assignment1.php .Item .See ./php_samples/assignment1.txt .Item Sample of assigning values to variables and doing some strange operations. .Row .See ./php_samples/block.php .Item .See ./php_samples/block.txt .Item Example of block scope and } is not required when ?> tag encountered. .Row .See ./php_samples/class_extends.php .Item .See ./php_samples/class_extends.txt .Item Sample showing how classes are inherited in PHP. .Row .See ./php_samples/class_sample.php .Item .See ./php_samples/class_sample.txt .Item Sample class of a class named Programmer. .Row .See ./php_samples/comment_sample.php .Item .See ./php_samples/comment_sample.txt .Item Sample showing how comments can be done in PHP. .Row .See ./php_samples/constant_sample.php .Item .See ./php_samples/constant_sample.txt .Item Sample showing how to define constants in PHP. .Row .See ./php_samples/const_in_class.php .Item .See ./php_samples/const_in_class.txt .Item Experiment to see what the scope of define'd constants are in PHP. .Row .See ./php_samples/define1.php .Item .See ./php_samples/define1.txt .Item A way to define a constant in PHP. Just displays a 1 - constant is defined. .Row .See ./php_samples/define2.php .Item .See ./php_samples/define2.txt .Item Another way to define a constant in PHP. Displays the value of the constant. .Row .See ./php_samples/define3.php .Item .See ./php_samples/define3.txt .Item An experiment in which 'a' contains the constant, and $b is 1. .Close.Table .Close PHP Samples