PHP syntax is generally similar to C/C++ (mixed with a bit of Perl, Java). One important difference is that PHP is not case-sensitive (although variable names are).
You can make PHP report errors when uninitialized variables are used by adjusting PHP's error reporting:
For example to print: Hello World
<?php $hello = "Hello World"; echo $hello; ?>PHP also picks up many variables from the environment. For example, $_SERVER['PHP_SELF'] , gives the path portion of the URL of the current file:
/tutorial/htmlguid/php/syntax.phtml.
$x = 10 //integer $y = 5.5 //float $z = (double) 10;
<php $hello = "Hello"; $world = "World"; $helloworld = "$hello $world"; $whoops = '$hello $world'; echo $helloworld, "<br>"; echo $whoops; ?>yields:
Hello World
$hello $world
$a[0] = "First";
$a[1] = "Second";
// we can add a third element without specifying an index
$a[] = "Third";
echo $a[0], $a[1], $a[2];
Giving:
First, Second, ThirdPHP also supports associative arrays. These can be declared either by using the
array function or explicitly as seen
below:
$assoc = array("Firstname" => "Robert",
"Lastname" => "Rittenhouse");
// alternatively
$assoc ["Midname"] = "George";
echo $assoc['Firstname'], " ", $assoc['Midname'], " ", $assoc['Lastname'];
Yielding: Robert George Rittenhouse
| example | name | result |
|---|---|---|
| $a + $b | Addition | Sum of $a and $b. |
| $a - $b | Subtraction | Remainder of $b subtracted from $a. |
| $a * $b | Multiplication | Product of $a and $b. |
| $a / $b | Division | Dividend of $a and $b. |
| $a % $b | Modulus | Remainder of $a divided by $b. |
| $s1 . $s2 | String concatenation | $s2 = "Hello " . "World"; //$s2 is "Hello World" |
| example | name | result |
|---|---|---|
| $a and $b | And | True of both $a and $b are true. |
| $a or $b | Or | True if either $a or $b is true. |
| $a xor $b | Xor | True if either $a or $b is true, but not both. |
| ! $a | Not | True if $a is not true. |
| $a && $b | And | True if both $a and $b are true. |
| $a || $b | Or | True if either $a or $b is true. |
Comparison operators, as their name imply, allow you to compare two values.
| example | name | result |
|---|---|---|
| $a == $b | Equal | True if $a is equal to $b. |
| $a != $b | Not equal | True if $a is not equal to $b. |
| $a < $b | Less than | True if $a is strictly less than $b. |
| $a > $b | Greater than | True if $a is strictly greater than $b. |
| $a <= $b | Less than or equal to | True if $a is less than or equal to $b. |
| $a >= $b | Greater than or equal to | True if $a is greater than or equal to $b. |
You can include files using the include or
require commands.
The files
in the directory /web/html/fragments/ can be included without
specifying a path. One useful file is a test for access from
McMurry which you can use with the statement <?php
require('ismcm.pinc'); ?>. This adds the function
isMcMurry(). and can be used as:
<php
echo $_SERVER['REMOTE_ADDR'] , " ";
if (isMcMurry() ){
echo " At McMurry ";
} else {
echo " Not McMurry ";
}
?>
prints: 38.107.191.81 IS At McMurry