PHP

What is PHP

PHP: Hypertext Preprocessor. PHP is a server side scripting language that is used in creating dynamic web pages. Often used with MySQL so that the PHP files can use the data stored in the databases. To create a PHP file you will need to save the file as a .PHP file then in the code start programming php with the tag: <?php and end with the tag ?> Everything inside those tags is read as php outside those tags the code is read as HTML.

For outputting onto a webpage using PHP there are 2 methods. First is a Echo statement.

<?php echo(“Hello World”) ?>

That would output Hello World onto the page. Also there is a print function to output to the webpage.

<?php print “Hello World” ?>

This will also output Hello World to the webpage. There is debate over which should be used for output. Echo seems to be faster in very large programs that just simply output text. To use quotes within a quoted text to be outputted to the webpage you have to use a backslash:

<?php print “Professor Eck said \"Hello World\" " ?>

Setting Variables you would have to start the variable name with a “$” then followed by a letter or an underscore. For example:

<?php $world = “Hello World!” ; print $world; $num = 123; print $num; ?>

Arrays in php are similar to java but instead of using integers as the index you can also use strings as the key or index for the array. Example:

<?php $friend[0] = "Justin"; $friend[1] = "Lloyd"; $friend[2] = "Alexa"; $friend[3] = "Devron"; $age["Justin"] = 45; $age["Lloyd"] = 32; $age["Alexa"] = 26; $age["Devron"] = 15; print "My friends names are " . $friend[0] . ", " . $friend[1] . ", " . $friend[2] . ", and " . $friend[3]; print "<p>"; print "Alexa is " . $age["Alexa"] . " years old"; ?>

Operators

Operators in PHP are similar to java including: +, -, /, *, <, >, ==, and !=. But for comparing variables you have to do it a little differently. For example:

$a = true; $b = true; $c = false; $a && $b; This is asking for $a and $b to both be true, since they are both true, this expression is TRUE $a || $b; This is asking for $a or $b to be true. Again this is a TRUE expression $a xor $b; This is asking for $a or $b, but not both, to be true. Since they are both true, this expression is FALSE ! $a; This is asking for $a to be false. Since $a is true, this expression is FALSE ! $c; This is asking for $c to be false. Since that is the case, this expression is TRUE

Working with MySQL

Working with SQL and connecting to databases in PHP you use

<?php mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ; mysql_select_db("Database_Name") or die(mysql_error()) ; ?>

I can use PHP to select data from our databases and create these tables:

SQL example

<?php // Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("Database_Name") or die(mysql_error()); $data = mysql_query("SELECT * FROM friends") or die(mysql_error()); Print "<table border cellpadding=3>"; while($info = mysql_fetch_array( $data )) { Print "<tr>"; Print "<th>Name:</th> <td>".$info['name'] . "</td> "; Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>"; } Print "</table>"; ?>

To insert rows into tables in PHP you would use:

SQL inserting rows example

<?php // Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("Database_Name") or die(mysql_error()); mysql_query("INSERT INTO tablename VALUES ( 'Bill', 29, 'Ford' ), ( 'Mike', 16, 'Beetle' ), ( 'Alisa', 36, 'Van' )"); Print "Your table has been populated"; ?>

Useful code in PHP

You can read in files using:

<?php $fh = fopen("myfile.txt", "r"); $file = file_get_contents("myfile.txt"); echo $file; ?>

But this will read the whole file and most likely hang up

To set a Cookie in PHP you would use

<?php setcookie("username", "john", time()+(60*60*24*365)); ?>

You can retrieve the cookie using

<?php echo $_COOKIE["username"]; ?>

Then you can output the cookie with

<?php echo "<pre>"; print_r($_COOKIE); echo "</pre>"; ?>

You can easily send emails using PHP as well using

Send Mail example

<?php mail("travis.pierce@hws.edu", "Hello", "Body of e-mail\r\nwith lines separated by the newline character."); ?>
Click here for more examples