[go: up one dir, main page]

0% found this document useful (0 votes)
34 views49 pages

Lec3 - Introduction To PHP

This document provides an introduction to PHP, covering its definition, usage, and advantages as a server-side scripting language. It outlines the installation of development environments like WAMP, MAMP, LAMP, and XAMPP, and explains PHP syntax, variables, and operators. The document serves as a foundational guide for beginners in web programming using PHP.

Uploaded by

mostafaferist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views49 pages

Lec3 - Introduction To PHP

This document provides an introduction to PHP, covering its definition, usage, and advantages as a server-side scripting language. It outlines the installation of development environments like WAMP, MAMP, LAMP, and XAMPP, and explains PHP syntax, variables, and operators. The document serves as a foundational guide for beginners in web programming using PHP.

Uploaded by

mostafaferist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Web Programming

02 Introduction to PHP
Dr. Mostafa Elgendy
2
Agenda

❖ Introduction to PHP

❖ Installing Environment

❖ PHP Syntax

❖ PHP Operators

❖ Summary

Web Programming 21-Oct-22


3

Introduction to PHP

Web Programming 21-Oct-22


4
URLs and web servers

❖ Usually when you type a URL in your browser:

❖ Your computer looks up the server's IP address using DNS

❖ Your browser connects to that IP address and requests the given file

❖ The web server software (e.g. Apache) grabs that file from the server's
local file system

❖ The server sends back its contents to you

Web Programming 21-Oct-22


5
URLs and web servers (cont.)

❖ Some URLs specify programs that the web server should run,
and then send their output back to you.

❖ Example: http://www.facebook.com/home.php

❖ This URL tells the server facebook.com to run the program home.php and
send back its output

Web Programming 21-Oct-22


6
Server-Side web programming

❖ Programs written using many web programming languages

❖ examples: PHP, Java/JSP, ASP.NET, Python, Perl

Web Programming 21-Oct-22


7
What is PHP?

❖ PHP stands for “Personal Home Page”

❖ PHP stands for "PHP Hypertext Preprocessor"

❖ Server-side scripting language

❖ PHP code can be embedded in HTML code

Web Programming 21-Oct-22


8
What is PHP?

❖ Used to make web pages dynamic:

❖ Provide different content depending on context

❖ Interface with other services: database, e-mail, etc.

❖ Authenticate users

❖ Process form information

Web Programming 21-Oct-22


9
Lifecycle of a PHP web request

Web Programming 21-Oct-22


10
Why PHP?

❖ Free and open source

❖ Compatible

❖ There were more than 19 million websites using PHP.

❖ Simple

Web Programming 21-Oct-22


11
Hello World!

<?php
echo "Hello, world!";
?>
PHP
Hello world!
output

Web Programming 21-Oct-22


12

Development Server

Web Programming 21-Oct-22


13
WAMP, MAMP, LAMP or XAMPP?

❖ WAMP: Windows, Apache, MariaDB, and PHP

❖ MAMP: Mac, Apache, MariaDB, and PHP

❖ LAMP: Linux, Apache, MariaDB, and PHP.

❖ XAMPP: cross-platform, Apache, MariaDB, PHP

Web Programming 21-Oct-22


14
WAMP, MAMP, LAMP or XAMPP?

❖ Download link:

❖ https://www.apachefriends.org/download.html

❖ Installing each component:

❖ Downloading and integrating the various parts yourself can be very time-
consuming

❖ Require a lot of research in order to configure everything fully

Web Programming 21-Oct-22


15
XAMPP

Web Programming 21-Oct-22


16
IDE

❖ Visual Studio Code (VSC)

❖ Runs on all of Windows, Mac,


Linux;

❖ Free

Web Programming 21-Oct-22


17
Write and Run first Program

❖ Open the directory where


XAMPP is installed.

❖ By default, XAMPP is installed in


the C:\ drive

Web Programming 21-Oct-22


18
Write and Run first Program

❖ Inside that “htdocs” folder,


create a folder and name it
anything suiting your project.

❖ Here, it has been named as


“lect2_app”.

Web Programming 21-Oct-22


19
Write and Run first Program

❖ Now open Visual Studio Code


and click on “open folder”

❖ Locate to C:\xampp\htdocs and


select the “lect2_app” folder
you created.

Web Programming 21-Oct-22


20
Write and Run first Program

❖ Create a file named “index.php”


inside the lect2_app folder.

❖ Since PHP is embedded into


HTML codes, write the following
HTML code along with the PHP
script.

Web Programming 21-Oct-22


21
Write and Run first Program

❖ Save the file and open your web


browser and type “localhost”
followed by the folder name that
you created and hit enter..

Web Programming 21-Oct-22


22

PHP Basic Syntax

Web Programming 21-Oct-22


23
PHP syntax template

❖ Contents of a .php file between <?php and ?> are executed as PHP code

❖ All other contents are output as pure HTML


HTML content
<?php
PHP code
?>
HTML content
<?php
PHP code
?>
PHP

Web Programming 21-Oct-22


24
Console output: print

<?php
echo "Hello, World!\n";
echo "Escape \"chars\" are the SAME as in Java!\n";
echo "You can have line breaks in a string.";
echo 'A string can use "single-quotes". It\'s cool!’;
?>
PHP

Hello world! Escape "chars" are the SAME as in Java! You can have line breaks in
a string. A string can use "single-quotes". It's cool!
output

Web Programming 21-Oct-22


25
PHP syntax template

❖ PHP is quite a simple language with roots in C and Perl.

❖ It is also very flexible,

❖ There are a few rules to learn about its syntax and structure

Web Programming 21-Oct-22


26
PHP syntax rules

❖ Semicolons: PHP commands ended with a semicolon, like this:


echo "Hello, world!";

❖ $: is used in front of all variables like this:$x += 10;

Web Programming 21-Oct-22


27
Comments

❖ Two ways to add comments to your PHP code.

❖ single line comment by using a pair of forward slashes.

❖ Multiple lines comment by using the /* and */ pairs of characters

// single-line comment
/*
multi-line comment
*/
PHP

Web Programming 21-Oct-22


28
Variables

❖ Names always begin with $, on both declaration and usage

❖ After the dollar sign, must start with a letter or the _ (underscore) character.

❖ Variable names can contain only the characters a–z, A–Z, 0–9, and _
(underscore).

❖ Variable names may not contain spaces. If a variable name must


comprise more than one word, a good idea is to separate the words
with the _ (underscore) character (e.g., $user_name).

Web Programming 21-Oct-22


29
Variables

❖ Variable names are case-sensitive. The variable $High_Score is not


the same as the variable $high_score

❖ Always implicitly declared by assignment (type is not written)

❖ A loosely typed language (like JavaScript or Python)

❖ This means that variables do not have to be declared before they are used.

❖ PHP converts variables to the type required by their context.

Web Programming 21-Oct-22


Variables 30

❖ Think of them as little (or big) matchboxes


that you’ve painted over and written names
on.
$name = expression; PHP

$username = "Fred Smith";


$age = 16;
$drinking_age = $age + 5;
$this_class_rocks = TRUE; PHP

Web Programming 21-Oct-22


31
Variables

❖ Example:

<?php // example2.php
$username = "Fred Smith";
echo $username;
echo "<br>";
$current_user = $username;
echo $current_user;
?>
PHP

Web Programming 21-Oct-22


32
Variables

❖ Basic types: int, float, boolean, string, array, object

❖ Test type of variable with is_type functions, e.g. is_string

❖ gettype function returns a variable's type as a string

Web Programming 21-Oct-22


33
Variables

❖ PHP is a loosely typed language.

❖ PHP converts between types automatically in many cases:

❖ string → int auto-conversion on +

❖ int → float auto-conversion on /

Web Programming 21-Oct-22


34
Variables

❖ Sometimes implicit casting may not be what you want.

❖ In division, by default PHP converts the output to floating point.

❖ But what if we want it to be an integer instead

❖ Explicit casting is the solution.

Web Programming 21-Oct-22


Variables: Arrays 35

❖ You can think of arrays as several


matchboxes glued together.

❖ $team = array('Bill', 'Mary', 'Mike', 'Chris',


'Anne’);

❖ If we then wanted to know who player 4


is, we could use this command:

❖ echo $team[3]; // Displays the name Chris

Web Programming 21-Oct-22


Variables: Two-dimensional arrays 36

❖ Arrays can be two-dimensional matrixes or even


have more dimensions.

❖ To then return the third element in the second row


of this array, use:

❖ echo $oxo[1][2];
<?php // example3.php
$oxo = array(array('x', ' ', 'o'),
array('o', 'o', 'x'),
array('x', 'o', ' '));
?>
PHP

Web Programming 21-Oct-22


37

PHP Operators

Web Programming 21-Oct-22


38
Operators

❖ Arithmetic operators:

❖ Let you specify mathematical operations to perform, such as addition,


subtraction, multiplication, and division.

❖ Other types of operators

❖ String, comparison, and logical operators.

Web Programming 21-Oct-22


Operators: Arithmetic operators 39

❖ Perform mathematics:

❖ The main four operations (add,


subtract, multiply, and divide)

❖ Find a modulus (the remainder


after a division)

❖ Increment or decrement a value.

Web Programming 21-Oct-22


Operators: Assignment operators 40

❖ These operators assign


values to variables.

Web Programming 21-Oct-22


Operators: Comparison operators 41

❖ Used inside a construct such


as an if statement in which
you need to compare two
items.

Web Programming 21-Oct-22


Operators: Logical operators 42

❖ Used to combine the results of


two of the comparison operators.

❖ For example, you might say to


yourself, “If the time is later than
12 p.m. and earlier than 2 p.m.,
have lunch.”

Web Programming 21-Oct-22


43
String types

❖ PHP supports two types of strings that are denoted by the type of
quotation mark that you use.

❖ To assign a literal string, preserving the exact contents use single-quoted:

❖ $info = 'Preface variables with a $ like this: $variable’;

❖ To include the value of a variable inside a string, use double-quoted:

❖ echo "This week $count people have viewed your profile";

Web Programming 21-Oct-22


44
String escaping characters

❖ Sometimes a string needs to contain characters with special


meanings that might be interpreted incorrectly.

❖ $text = 'My spelling's atroshus'; // Erroneous syntax

❖ To correct this, you can add a backslash directly

❖ $text = 'My spelling\'s still atroshus’;

❖ $text = "She wrote upon it, \"Return to sender\".";

Web Programming 21-Oct-22


45
String Functions

Name Java Equivalent


strlen length
$name = "Stefanie Hatcher";
$length = strlen($name); strpos indexOf
$cmp = strcmp($name, "Brian Le"); substr substring
$index = strpos($name, "e");
toLowerCase,
$first = substr($name, 9, 5); strtolower, strtoupper
toUpperCase
$name = strtoupper($name);
PHP trim trim
explode, implode split, join
strcmp compareTo

Web Programming 21-Oct-22


46
Math operations

$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));
PHP

abs ceil cos floor log log10 max


min pow rand round sin sqrt tan

Web Programming 21-Oct-22


47
Constants

❖ Constants are similar to variables,

❖ holding information to be accessed later.

❖ Once defined, its value is set for the remainder of the program and cannot be altered.

❖ Example: define("ROOT_LOCATION", "/usr/local/www/");

❖ Must not be prefaced with a $ (unlike regular variables). Just use the define
function.

Web Programming 21-Oct-22


48
Summary

❖ Introduction to PHP

❖ Installing Environment

❖ PHP Syntax

❖ PHP Operators

❖ Summary

IS388 - Web Programming 21-Oct-22


Questions

You might also like