-
Client - Server
- The Client (The browser):
- request a Web page
- retrieve (all of) it
- render it
- The Server:
- Stores html, media, image etc. files
- Connected to the WWW
- Waits for requests from browsers
- Locates and sends a resource on request
Web Server
Responds to client requests by providing resources
URL (Uniform Resource Locator)
- Can access Database Management System (DBMS)
- e,g MySQL - a popular choice of database for use in web applications,
- Server-side scripts
- Executed on server
- Generate custom response for clients
- Wide range of programmatic capabilities
- Access to server-side software that extends server functionality
X- Client-side scripts
- Validate user input
- Reduce requests needed to be passed to server
- Access browser
- Enhance Web pages with HTML (HTML5) / CSS / Javascript
-
PHP
PHP: Hypertext Preprocessor
Server-side scripting language
Open source
Free
Script runs on server to produce html
.php files
A PHP script is executed on the server
The plain HTML result is sent back to the browser.
A PHP script can be placed anywhere in the document.
- PHP code is executed on the server
- generating HTML which is then sent to the client.
The client would receive the results of running that script, but would not know what the underlying code was.
- What Can PHP Do?
- PHP can generate dynamic page content
- PHP can create, open, read, write, delete, and close files on the server
- PHP can collect Form data
- PHP can add, delete, modify data in your database
- PHP can be used to control user-access
- PHP can encrypt data
-
Introduction:
Hello World - 💡 HelloWorld.php
echo used to output text onto a web page:
<html> <body> <h1> PHP Example (This is HTML ) </h1> <?php echo "Hello World (This is PHP) "; ?> </body> </html>
To execute the PHP code:
- A php file MUST be run on a Web Server
- e.g Xampp
- Note the Server Address – for example :
- localhost/...... /HelloWorld.php
Server Address (URL)
- If it is run on the Client only :
- PHP code will NOT be executed
- Will ONLY view HTML
PHP
PHP is a scripting language ( as is Javascript)
- PHP is Loosely typed language (similar to Javascript)
- Variable automatically converted to correct data type when to fit assigned value
Variables and Types
Variables start with a $ symbol followed by a letter or underscore and other alpha_numeric characters – no spaces
$color = “purple”;
PHP IS Case –Sensitive for Variable Names :
<?php $color = "red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?>
$COLOR and $coLOR are DIFFERENT variables – they have NOT been declared – so will NOT be displayed
Operators
Arithmetic
+ - * / % ++ --
Assignment
= += -= *= /= %= .=
Comparison
== != <> > < >= <=
Logical
&& || !
-
PHP String Operations – 💡 strings.php
. - concatenate …a full-stop !!
All of these will output : My name is Brian Shields
<?php echo "My name is " . " Brian Shields "; // join two text strings $fname = "Brian"; echo "My name is " . $fname . " Shields"; // join two text strings and variable (text string) $sname = "Shields"; echo "My name is " . $fname . " " . $sname; // join a text string and two variables (text strings ) - also need to add a space character $fullname = $fname . " " . $sname; // create a variable (text string) from two other text strings echo "My name is " . $fullname; $fulltext = "My name is " . $fname . " " . $sname; // create a variable (text string) which contains all text - including variables echo $fulltext; ?>
Other Functions
str_word_count //counts the number of words in a string
strtoupper //convert string to UPPER CASE
str_shuffle //shuffles a string
str_replace //Replace all occurrences of the search string with the replacement string
Other Functions cont...
<?php $fname = "Fred"; $sname = "Smith"; $str = $fname . " " . $sname; echo "The string is : " . $str; echo "<br/>"; $num_words = str_word_count($str); echo "Word Count of the string is : " . "$num_words"; echo "<br/>"; $upper_word = strtoupper("$name"); echo "Surname upper case is: " . "$upper_word"; echo "<br/>"; $shuffled_name = str_shuffle("$sname"); echo "The shuffled surname is : " . $shuffled_name; echo $str2; echo "<br/>"; $str_new = str_replace("Fred", "Mary", $str); echo "Fred has been replaced by Mary and the new string is :" . $str_new; echo "<br/>"; ?>
-
Selection
If
if (condition) code to be executed if condition is true;
if ... else
if (condition) code to be executed if condition is true; else code to be executed if condition is false;
if ... elseif ... else
if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false;
Switch
switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; }
Iteration
while
while (condition) { code to be executed; }
do ... while
do { code to be executed; } while (condition);
for
for (init; condition; increment) { code to be executed; }
foreach
foreach ($array as $value) { code to be executed; }
Arrays
Numeric
$cars=array("Saab","Volvo","BMW","Toyota"); $cars[0]="Saab"; $cars[1]="Volvo";
Associative - each ID key is associated with a value
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $ages['Peter'] = "32"; $ages['Quagmire'] = "30";
-
Functions
script automatically executes when a page is loaded except if it is in a function
function functionName() { code to be executed; }
function can be called from anywhere in page
<?php function writeFullName() { echo “Brian Shields"; } echo "My name is "; writeFullName(); ?>
Output: My name is Brian Shields
Parameters
<?php function writeNames($fname) { echo $fname . " Smith.<br />"; } echo "My name is "; writeNames(“Fred"); echo "My sister's name is "; writeNames(“Jean"); echo "My brother's name is "; writeNames(“John"); ?>
Output:
My name is Fred Smith
My sister's name is Jean Smith
My brother's name is John SmithReturn Value
<?php function add($x,$y) { $total=$x+$y; return $total; } //Note the use of String Concatenation echo "1 + 16 = " . add(1,16); ?>
Output: 1 + 16 = 17
Video
X