This page contains all of the code samples for PHP.
For a quick, concise reference to terms used through out this page, see [ php.glossary.html ] which contains meta-linguistic terms, lexemes, and keyword definitions based on the C++ glossary found at [ c++.glossary.html ] with the appropriate adaptations to PHP.
<?php
/* Initialize some variables using C style comments
$a - contains a-coefficient
$b - contains b-coefficient
$x - value we are evaluating
$y - result from evaluating equation */
$a = 1; $b = 2; $x = 1;
$y = $a * $x + $b;
if ( $y < 5 ) // C++ style comment, check if y<5
{
# Shell style comment, display something when y<5
echo "Guess what? y is less than 5!";
}
?>
To see this script in action, visit [ php_samples/comment_sample.php ] .
<?php
$count = 0; // Set count equal to 0
$_xval = 5;
$_yval = 1.0;
$some_string = "Hello There!";
$will i work=6; // This does not work!!!
$3blindmice=3; // Same here, does not work
?>
To see this script in action, visit [ php_samples/variable_sample.php ] (script does not include the two lines that will fail).
<?php
function m_x_plus_b($m, $x, $b) {
return $m*$x+$b;
}
$slope=1.5;
$x=2;
$b=3;
echo "y = ".m_x_plus_b($slope,$x,$b)."<br>";
?>
To see this script in action, visit [ 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 [ php_samples/function_sample2.php ] .
<?php
// This is a sample script from Paul Conrad's Independent Study
// This script will call the function named foo showing case insensitivity.
function foo($my_string) {
echo "$"."my_string = $my_string<br>";
}
foo("foo is foo");
Foo("Foo is foo");
FoO("FoO is foo");
FOO("FOO is foo");
?>
The two following sample scripts show two different ways of using the same anonymous function.
<?php
$lambda=create_function('$a,$b','return(strlen($a)-strlen($b));');
$array=array('really long string here, boy','middling length','larger');
usort($array,$lambda);
print_r($array);
?>
To see this script in action, visit [ php_samples/anonymous_function.php ] .
<?php
$array=array('really long string here, boy','middling length','larger');
usort($array,create_function('$a,$b','return(strlen($a)-strlen($b));'));
print_r($array);
?>
To see this script in action, visit [ php_samples/anonymous_function2.php ] .
<?php
class Programmer {
// Class Properties
var $name; // Programmer's name
var $experience; // How long has been programming
var $lang; // Favorite Language
var $education; // Highest degree earned
// Class Constructor - function same name as the class
function Programmer($name, $experience, $lang, $education) {
$this->name=$name;
$this->experience=$experience;
$this->lang=$lang;
$this->education=$education;
}
// Getter/Setter functions for all properties in the class
function get_name() {
return $this->name;
}
function set_name($newname) {
$this->name = $newname;
}
function get_experience() {
return $this->experience;
}
function set_experience($newexperience) {
$this->experience = $newexperience;
}
function get_lang() {
return $this->lang;
}
function set_lang($newlang) {
$this->lang = $newlang;
}
function get_education() {
return $this->education;
}
function set_education($neweducation) {
$this->education = $neweducation;
}
// Utility data dump function
function output() {
echo "Programmer Name: ".$this->name."<br>";
echo $this->name." has ".$this->experience." years of programming experience.<br>";
echo $this->lang." is ".$this->name."'s favorite programming language.<br>";
echo $this->name." holds the degree: ".$this->education."<br><br>";
}
}
// Instantiating a programmer
$paul = new Programmer('Paul Conrad',12,'C++','Bachelor of Science in Computer Science');
$paul->output();
// Oops, Paul has programmed alot longer than 12 year, really is 22 years
$paul->set_experience(22);
$paul->output();
?>
To see this script in action, visit [ php_samples/class_sample.php ] .
<?php
define('school',"California State University at San Bernardino");
define('programs',4);
echo "Our school is ".school."<br>";
echo "Our school's Computer Science Dept has ".programs." academic programs";
?>
To see this script in action, visit [ php_samples/constant_sample.php ] .
PHP Sample Code Quick Reference
| Example PHP Script | Source File | Description |
|---|---|---|
| [ php_samples/anonymous_function.php ] | [ php_samples/anonymous_function.txt ] | Sample of anonymous functions at work. |
| [ php_samples/anonymous_function2.php ] | [ php_samples/anonymous_function2.txt ] | Another anonymous function sample, no assigned variable, though (YAGNI). |
| [ php_samples/assignment1.php ] | [ php_samples/assignment1.txt ] | Sample of assigning values to variables and doing some strange operations. |
| [ php_samples/block.php ] | [ php_samples/block.txt ] | Example of block scope and } is not required when ?> tag encountered. |
| [ php_samples/class_extends.php ] | [ php_samples/class_extends.txt ] | Sample showing how classes are inherited in PHP. |
| [ php_samples/class_sample.php ] | [ php_samples/class_sample.txt ] | Sample class of a class named Programmer. |
| [ php_samples/comment_sample.php ] | [ php_samples/comment_sample.txt ] | Sample showing how comments can be done in PHP. |
| [ php_samples/constant_sample.php ] | [ php_samples/constant_sample.txt ] | Sample showing how to define constants in PHP. |
| [ php_samples/const_in_class.php ] | [ php_samples/const_in_class.txt ] | Experiment to see what the scope of define'd constants are in PHP. |
| [ php_samples/define1.php ] | [ php_samples/define1.txt ] | A way to define a constant in PHP. Just displays a 1 - constant is defined. |
| [ php_samples/define2.php ] | [ php_samples/define2.txt ] | Another way to define a constant in PHP. Displays the value of the constant. |
| [ php_samples/define3.php ] | [ php_samples/define3.txt ] | An experiment in which 'a' contains the constant, and b is 1. |
. . . . . . . . . ( end of section PHP Samples) <<Contents | End>>