0% found this document useful (0 votes)
9 views

Intro To PHP

Uploaded by

Quang Đại
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Intro To PHP

Uploaded by

Quang Đại
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Introduction to PHP

Matt Prichard – comp 1841


The Beginning
• PHP originally stood for "Personal Home Page".
They were a set of Perl scripts written by an IT
consultant (R.Lerdorf) in order to keep track of
visitors to his home page (1994).

• Now stands for: PHP: Hypertext Preprocessor


• It is a server-side scripting language
Why Scripting Languages ?
• HTML has limitations as a tool
• Can only deal with text formatting/content organisation
• It is static: Interactivity is not possible

• Programming tools enhance static web pages


• Creating conditional structures
• Time based or event driven interactions
• Link dynamically to databases
Application Areas
• Read & write files
• Process forms
• Send data via e-mail
• Access & manipulate database records
• Read & write cookies
• Facilitate user authentication
• Provide data encryption
Things PHP Cannot Do
• PHP operates server-side and cannot provide
interactivity on the client.
• PHP cannot
• Create a new browser window
• Produce mouse-over effects
• Produce Pop-ups
• Resize browser windows
• JavaScript can provide this client-side interactivity
PHP.NET
Web Server Environments
• PHP runs on virtually all Web server
environments, including the following key ones:
• Microsoft’s IIS (Internet Information Services) –
• On the University I-drive
• Apache – which is by far the most widely used
commercial Web server as is part of XAMPP
Getting Started
To run PHP code
1. You need a server environment (eg localhost or your
I-drive)
2. You must make sure the PHP parser is installed
3. You create an HTML type file
4. Use <?php … ?> tags to enter your PHP script
5. Save your file with extension .PHP
6. View it in your browser via http from your server.
7. http://stuiis.cms.gre.ac.uk/****
8. Or http://localhost:8080/
9. It will not run from your G drive, USB key or if
you open it in browser from your I drive or
desktop.
phpinfo()
Enter the following code into a document, it uses the
php info function
<?php
phpinfo();
?>
Save file as info.php on your I drive. Go to for
example:
http://stuiis.cms.gre.ac.uk/pm76/info.php
to see lots of details about your current install.
Or http://localhost/COMP1841/info.php
Hello World

http://localhost/COMP1841/week1/hello.php
View Source hello.php
Hello Bold

http://localhost/COMP1841/week1/hello_bold.php
View Source hello_bold.php
Some Syntax Considerations
• PHP is not case sensitive.
• Hence ECHO is the same as echo
• But pick a system and stick to it, I go
with lower case.
• PHP sees any code after // on a line or
between /* and */ as a comment.
comments
Reserved Words
PHP contains reserved words like JavaScript and most
other languages and these can not be used as
variable names. Some examples:
• break
• case
• continue
• do
• echo()
• empty()
• if … else … elseif … endif
Variables & Data types
• All variables in PHP start with a $(dollar) sign. For example:

• PHP variables are loosely typed and can store different


types of data. You are not required to declare a variable as a
particular data type.
• This is similar to JavaScript and Python but not languages
such as C++, C# and Java which are strongly or statically
typed.
Data types cont…
• PHP can store the following data types in its
variables:
• String - $my_string = "This is a string"
• Integer - $num = 56
• Floating point - $flo_point = 6.8734
• Boolean - $bol = TRUE
• Additionally a variable could be set to have no
value. For example:
• $no_val = NULL
DataTypes.php

http://localhost/COMP1841/week1/DataTypes.php
Print and Echo
• These built in functions are used to send data to
the browser, or write to the screen.
• echo( )
• print( )
• Both work the same, I use echo( ) the most, you
may see print( ) in other examples
• You can use single quotes or double quotes in or
out of brackets.
• Begin by using echo and double quotes
Print and echo cont…
Single or double quotes
Values in single quotes are treated literally, those in
double quotes are interpreted, this is called variable
interpolation. Consider this code:

NB: you can also echo


a variable directly with
no quotes or brackets.
( see slide 27)
http://localhost/COMP1841/week1/single_double.php
Escaping quotes
Problem:
echo “she said, “how are you ?””;

http://localhost/COMP1841/week1/escape_error.php
PHP Functions

http://localhost/COMP1841/week1/function.php
Multiple Arguments

http://localhost/COMP1841/week1/function_multi.php
Arithmetical Operators
String concatenation

http://localhost/COMP1841/week1/concat.php
Logical Operators in PHP
Assignment Operators
Comparison Operators
Date and Time Functions
• PHP provides a predefined Date and Time function that
can be used to time stamp events or display relevant
messages to users (e.g. based on time or day of the week)
• Date (format string)
• key format strings :
• D - textual representation of a day, three letters i.e. Mon to Sun;
• F - for full months of the year from January to December and
• H - full 24 hours formatted clock – please note that there is
difference between H and h string or D and d string etc. )
• More at http://www.w3schools.com/php/php_date.asp
Date and Time example

http://localhost/COMP1841/week1/date.php
Dynamic copyright

http://localhost/COMP1841/week1/copy.php
Control Structures
Conditional statements or if statements
Conditional statements or if statements

https://www.w3schools.com/php/php_if_else.asp
Loops - The for loop – when we know up
front how many times we need to run the same code
For Loop

https://www.w3schools.com/php/php_looping_for.asp
The while loop - Loops through a block of code as long as
the specified condition is true. Like repeating if statements.
While Loop

https://www.w3schools.com/php/php_looping_while.asp
Foreach Loop

https://www.w3schools.com/php/php_looping_foreach.asp
Any Questions

You might also like