2. Variables
A variable is used to represent a piece
of information in terms that the
computer can understand
Types of variables include:
◦ Integers = Numbers
◦ Strings = Letters and Numbers
◦ Boolean = True / False
◦ Array = Variable that contains many
variables
3. Syntax
Syntax is simply the language of the
program
Syntax is the grammar used to get the
program to run properly
Without proper grammar the program
is doomed to fail!
4. Syntax Cont.
Let‟s start a PHP file
◦ Begin every PHP file by opening a PHP
bracket
◦ To open the bracket simply start with-
<?PHP
◦ To end the bracket type- ?>
◦ Ending Bracket = <?PHP ?>
◦ The code goes in-between
5. Syntax Cont.
Variable Syntax-
◦ How we declare a variable
◦ Begin every variable with a dollar sign $
◦ Next we add the name- $variable
◦ Finally we give it a value- $variable = 50;
Always remember to end all of your
statements with a semicolon; It is how
the program knows the command is
over
6. Syntax Cont.
How to make sure you have declared
your variable properly
◦ Echo = very useful PHP function
◦ Echo to show the value of your variable
◦ Ex-
$variable = 5;
Echo $variable;
Output on the web page = 5
7. Databases
Let‟s connect to a database!
Start by declaring variables-
◦ $connection = mysql_connect();
This variable is using the mysql_connect
function to aid us in the connection.
This function requires 3 inputs within the ().
Server Name
User Name
Password
◦ $connection = mysql_connect(„localhost‟,
„root‟, „password‟);
9. Database Language
Now we just need to learn a little
MYSQL to work with databases. Don‟t
worry, it‟s not hard.
Pull the information from a table
◦ Select all from Table_name(„field_name‟);
◦ Now you have all the information from that
table.
◦ Put it in PHP-
$query = “SELECT all from TABLE („field‟,
„field‟, field‟);”;
10. PHP and the Web
One more vital function for web work
If / Else statements
Based on a certain condition
If X = 1 than go left, If X = 2 go right.
Syntax-
◦ If(condition){outcome};
11. PHP and the Web
Let‟s put it all together!
◦ Declare your database variables
◦ Open up your PHP bracket
◦ Time to generate dynamic content!
12. Dynamic Content
Use logic and functions to think up
endless possibilities
Let‟s start with random pictures
Important function to making this work
is the Rand() function
◦ Rand() Generates a random number
between any number you give it.
Ex- Rand(1,100) will not generate a random
number between 1 and 100.
13. Dynamic Content Cont
Get some images
◦ To make this work be sure to end each file
name with a number. Pic1.jpg, pic2.jpg,
etc etc
Declare a variable with rand()
◦ $random = rand(1,3);
14. Dynamic Content Cont
Put it all together
◦ <?php
◦ $random = rand(1,3);
◦ Echo “Img src=„image” . $random “.jpg‟>”;
◦ ?>
15. Dynamic Content Cont
So what happened?
◦ We declared a variable for a random
number
◦ We used the echo function to show the
image
◦ Now every time this runs it will generate a
random number, place the number at the
end of the image and show that image
16. End
Now you should have a very basic
understanding of the PHP language.
There is a lot more out there to learn
about PHP and programming.
If you want to learn more visit
www.php.net