Cpsc110/Notes/Starting PHP

This page provides a list of topics/notes on the material from Chapter 2 of Learning PhP and MySQL:

PHP and HTML
PHP is embedded in HTML code by enclosing it within the  tags:   Hello World   This was printed by PHP."; ?>  

Comments
Comments are used to document and clarify your code:   Hello World   <?php // This is a single line comment.

/*        * This is a multiline comment. */       echo "This was printed by PHP.</P>"; ?> </BODY> </HTML>

Variables
A variable is a memory location that stores a value of a particular type (a string such as "Hello" or a number such as 32). In PHP variable names begin with $ and are defined as follows: $variable_name = 54 Variable names in PHP are case sensitive and must start with dollar sign ($) and must be followed by a letter, and may consist of letters, digits and the underscore (_) character. A variable is assigned a value using the equal sign (=).

Variable Type
Variables in PHP are loosely typed (different from Java). A variable's type is automatically picked by PHP when you assign it a value. If you assign it a number, then its type is numeric (or integer). PHP automatically performs certain conversions--e.g., "20"/2 = 10.

Variable Scope
Unless otherwise declared, a variable's scope is limited to the function (named block of code) or block of code where it is defined. A variable cannot be used outside its scope. <?php // Define a function function birthday { $age = 1; // Set local variable age to 1 }

$age = 30;  // Set age to 30 birthday; // Call the function echo $age;  // This will print 30 ?>

A global variable has a scope that extends beyond these boundaries--its value will be the same wherever you define it as global: <?php // Define a function function birthday { global $age = 1; // Set global variable age to 1 $age = $age + 1; // Add 1 to age }

$age = 30;  // Set age to 30 birthday; // Call the function echo $age;  // This will print 31 ?> A static variable is one whose value is preserved between different function calls. <?php // Define a function function birthday { static $age = 0; // Set static variable age to 1 $age = $age + 1; // Add 1 to age echo "Birthday number $age<br ?>";  // Print its value }

$age = 30;  // Set age to 30 birthday; // Call the function-- prints 1 birthday; // Call the function again -- prints 2 echo "Age: $age<br ?>";  // This will print 30 ?>

Super Globals
PHP also has certain super globals such as $_SERVER and $_GET, that provide information about the script's environment.

String Examples
<?php $my_string = "Margaritaville - Suntan Oil Application!"; echo "Margaritaville - Suntan Oil Application!";

echo "Time for $my_string";

$my_string = 'Margaritaville - Suntan Oil Application!'; echo $my_string;

Escape Characters
<?php //  \n (newline), \r (carriage return), \t (tabl), \$ (dollar sign), \" (double quote)

echo "<h2 class=\"specialH2\"Margaritaville! "; echo '<h2 class="specialH2"Margaritaville! '; ?>

Comparing Strings
<?php $name1 = "Bill"; $name2 = "BIll"; $result = strcasecmp($name1, $name2); // Compare but ignore case, returns 0 if (!$result) { echo "They match";  // This will print in this case, because != 0 }

$result = strcmp($name1, $name2); // Compare but include case, returns non-zero if ($result) { echo "They match";  // This will also print in this case b/c $result is non-zero } ?> Other string comparison operators include:

(equal),
(identical), != (not equal), <> (not equal), < (less than), > (greater than), <= (less equal to), >= (greater equal to)

String Concatenation
Strings can be concatenated by using the period (.) operator: <?php $str = "This is an example of ". 3 . " in the middle of a string"; echo $str; ?>

Click here for the in class example.