GRAF 5016 - Week 6

Today

PHP: Hypertext Preprocessor, an open source, server-side, HTML embedded scripting language used to create dynamic Web pages.
In an HTML document, PHP script (similar syntax to that of Perl or C ) is enclosed within special PHP tags. Because PHP is embedded within tags, the author can jump between HTML and PHP (similar to ASP and Cold Fusion) instead of having to rely on heavy amounts of code to output HTML. And, because PHP is executed on the server, the client cannot view the PHP code.

PHP can perform any task that any CGI program can do, but its strength lies in its compatibility with many types of databases.

some PHP information found at: http://www.tizag.com/

PHP - What's it do?

* Reduce the time to create large websites.
* Create a customized user experience for visitors based on information that you have gathered from them.
* Open up thousands of possibilities for online tools.
* Allow creation of shopping carts for e-commerce websites.

How to Save Your PHP Pages

If you have PHP inserted into your HTML and want the web browser to interpret it correctly, then you must save the file with a .php extension, instead of the standard .html extension. So be sure to check that you are saving your files correctly. Instead of index.html, it should be index.php if there is PHP code in the file.

<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>

whitespace in php code - as with html - is ignored

and the end of every statement is concluded with a semicolon ;

Echo is not a function, rather it is a language construct.

Php Variables

A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:

$variable_name = Value;

If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.

<?php
$hello = "Hello World!";
$a_number = 4;
$anotherNumber = 8;
?>

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

* PHP variables must start with a letter or underscore "_".
* PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
* Variables with more than one word should be separated with underscores. $my_variable
* Variables with more than one word can also be distinguished with capitalization. $myVariable

Outputting a String

To output a string, like we have done in previous lessons, use PHP echo. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.

<?php
$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";
?>

Don't use quotes inside your string

Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \"

Use single quotes (apostrophes) for quotes inside your string.

<?php
// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">I love using PHP!</h5>";

// OK because we escaped the quotes!
echo "<h5 class=\"specialH5\">I love using PHP!</h5>";

// OK because we used an apostrophe '
echo "<h5 class='specialH5'>I love using PHP!</h5>";
?>

Echoing Variables and Text Strings

You can also place variables inside of double-quoted strings (e.g. "string here and a $variable"). By putting a variable inside the quotes (" ") you are telling PHP that you want it to grab the string value of that variable and use it in the string. The example below shows an example of this cool feature.

<?php
$my_string = "Hello Bob. My name is: ";
echo "$my_string Bobettta <br />";
echo "Hi, I'm Bob. Who are you? $my_string <br />";
echo "Hi, I'm Bob. Who are you? $my_string Bobetta";
?>

$my_string = "Tizag - Unlock your potential!";
echo "Tizag - Unlock your potential!";
echo $my_string;

you can also use single quotes:

echo 'Tizag - It\'s Neat!';

Basic Math operators in PHP

+ : The addition operator
- : The subtraction operator , can also be used for negation like this -9
* : The multiplication operator
/ : the division operator
% : the modulus operator , returns the remainder after division . For example 25 % 3 would give us 1 .

<?php
$number1 = 9;
$number2 = 4;
echo $number1 + $number2;
?>

This returns the number 13

which is the same as saying $mybill = $mybill + 20;

Order Of Precedence

The math operators all have to obey an order of precedence which is used in mathematics.

$calc = 25 + 4 * 2;

What do you think the value of $calc is , well you could add 25 + 4 and get 29 then multiply by 2 and get 58 . Wrong. The rules of precedence say that multiplication gets calculated before addition so you get 4 * 2 is 8 and then 25 + 8 = 33. 33 is the correct answer. A big difference from 58 .

At school I was taught to use BODMAS to help remember order of precedence this is basically Brackets , Division , Multiplication , Addition , Subtraction. What if you want to add the 25 + 4 first and then multiply this by 2 , well you can do the following.

$calc = (25 + 4) * 2;

Now the brackets get evaluated first because they come before multiplication in the order of precedence..

Comments

single line:

echo "Hello World!"; // This will print out Hello World!

multiline comments:

/* This Echo statement will print out my message to the
the place in which I reside on. In other words, the World. */
echo "Hello World!";

If / Else

Someone comes to your website and you want to ask this visitor her name if it is her first time coming to your site. With an if statement this is easy. Simply have a conditional statement to check, "are you visiting for the first time". If the condition is true, then take them to the "Insert Your Name" page, else let her view the website as normal because you have already asked her for her name in the past.

 

$number_three = 3;
if ( $number_three == 3 ) {
echo "The if statement evaluated to true";
} else {
echo "The if statement evaluated to false";
}

returns:

The if statement evaluated to true

* We first made a PHP variable called $number_three and set it equal to 3.
* In this example we compared a variable to an integer value. To do such a comparison we use "==", which in English means "Is Equal To".
* $number_three is indeed Equal To 3 and so this statement will evaluate to true.
* All code that is contained between the opening curly brace "{" that follows the if statement and the closing curly brace "}" will be executed when the if statement is true.
* The code contained within the else segment will not used.

Here is what would happen if we changed to $number_three to anything besides the number 3.

$number_three = 421;

if ( $number_three == 3 ) {
echo "The if statement evaluated to true";
} else {
echo "The if statement evaluated to false";
}

returns:

The if statement evaluated to false

Php Includes

files

download the following folder for practice

example

Open index.html from your basicPhp folder

Create a new external style sheets with the following rules:

* { padding: 0; margin: 0; border: 0; border: 0; outline: 0; font-size: 100.1%; }

body {
font-family:Verdana, Geneva, sans-serif;
color:#666;
background-color: #DEF6FF;
}

#wrapper {
width: 980px;
margin-right: auto;
margin-left:auto;
font-size:12px;
}

back in your index...code view, enter the following after the start of the wrapper div:

<?php include("banner.php"); ?>

Style your banner:

#banner {
background-image: url(images/8012_Industrial_Sabotage.jpg);
background-repeat: no-repeat;
height: 275px;
background-position: center -75px;
}

now your nav include (after the banner):

<?php include("nav.php"); ?>

style your nav:

#nav {
background-color: #CCC;
padding: 15px;
text-align: center;
}

#nav a {
display: block;
width: 150px;
font-size: 12px;
font-weight: bold;
color: #FFF;
background-color: #4C49A0;
padding: 10px;
text-decoration: none;
float: left;
margin-left: 17px;

Lastly, add your footer after the content div:

<?php include("footer.php"); ?>

and style it:

#footer {
background-color: #CCC;
padding: 5px;
clear: both;
text-align: center;
}
#footer a {
font-size: 8px;
color: #4E499F;
}

and some additional styles:

#content {
padding: 30px;
background-color: #FFF;
}
.clearBoth {
clear: both;
}

that's it - look at how little html code you have! This will make updating your site more efficient.