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

Chapter 5 (Introduction to php)-2

Php

Uploaded by

Ayman Nabil
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)
5 views

Chapter 5 (Introduction to php)-2

Php

Uploaded by

Ayman Nabil
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/ 31

Introduction to PHP

Chapter 5
Topics Covered
⚫ PHP - what is it?
⚫ What does it do?
⚫ PHP Language basics
⚫ Syntax and structure
⚫ Variables and operators
⚫ Data types and conversions

2
Introduction
⚫ PHP is a general-purpose scripting
language geared towards web development.
⚫ It was originally created by Danish-
Canadian programmer Rasmus Lerdorf in
1993 and released in 1995
⚫ PHP was originally an abbreviation
of Personal Home Page, but it now stands
for PHP: Hypertext Preprocessor.

3
PHP
⚫ > PHP is a server-side scripting language
(PHP scripts are executed on the server)
⚫ > PHP supports many databases (MySQL,
Oracle, Sybase, PostgreSQL etc.)
⚫ > PHP is open source software (PHP is free to
download and use)

4
Science Calculations

System

System

C uses curly
braces { } for
code blocks.
Scripting/
Interpreted

http://en.wikipedia.org/wiki/History_of_programming_languages

5
About the PHP Language
• Syntax inspired by C
- Curly braces, semicolons …
• Syntax inspired by perl
- Dollar signs to start variable names …
• Extends HTML to add segments of PHP
within an HTML file

6
Version Release date Supported until
Release 1 8-Jun-95
2 1-Nov-97
History 3 6-Jun-98 20 October 2000
4 22 May 2000 23 June 2001
4.1 10 December 2001 12 March 2002
4.2 22 April 2002 6 September 2002
4.3 27 December 2002 31 March 2005
4.4 11 July 2005 7 August 2008
5 13 July 2004 5 September 2005
5.1 24 November 2005 24 August 2006
5.2 2 November 2006 6 January 2011
5.3 30 June 2009 14 August 2014
5.4 1 March 2012 3 September 2015
5.5 20 June 2013 10 July 2016
5.6 28 August 2014 31 December 2018
6.x Not released —
7 3 December 2015 10 January 2019
7.1 1-Dec-16 1 December 2019
7.2 30-Nov-17 30 November 2020
7.3 6 December 2018 6-Dec-21
7.4 28 November 2019 28-Nov-22
8 26 November 2020 26-Nov-23
8.1 25 November 2021 25-Nov-24
8.2 8 December 2022 8-Dec-25 7
8.3 23 November 2023 23-Nov-26
Over 77% of all active and live
websites in the world use PHP

8
PHP Demographics Statistics

9
Usage Statistics
⚫ These are the specific number of PHP-powered
websites in each territory:
✓ United States – 1,793,563
✓ Germany – 1,731,749
✓ Russia – 1,389,697
✓ United Kingdom – 1,017,478

10
PHP Trends Statistics
• PHP – 77.5%
• ASP.NET – 7.5%
• Ruby – 5.6%
• Java – 4.5%
• Scala – 2.7%
• JavaScript – 2.2%
• Python – 1.4%
• ColdFusion – 0.3%
• Perl – 0.1%
11
Popular Sites Using PHP
o Microsoft.com
o Facebook.com
o Wikipedia.org
o Wordpress.org
o Zoom.com
o Vimeo.com

12
Popularity
⚫ PHP is used for Web content management
systems including:
o MediaWiki,
o WordPress,
o Joomla,
o Moodle,
o eZ Publish,
o eZ Platform,
o etc... 13
PHP Frameworks
⚫ PHP has also attracted the development of
many software frameworks that provide
building blocks and a design structure to
promote rapid application development (RAD).
⚫ Some of these include
⚫ PRADO, CakePHP, Symfony, CodeIgniter, Laravel,
Yii Framework, Phalcon and Laminas.

14
PHP Deploying
⚫ There are many packages become
popular in the web industry as a way of
deploying PHP web applications such as:
⚫ LAMP – Linux, Apache, MySQL, PHP
(P may also refer to Python, Perl)
⚫ WAMP – Windows, Apache, MySQL, PHP
⚫ MAMP – Mac, Apache, MySQL, PHP
⚫ XAMPP – Cross-platform (X), Apache, MariaDB,
PHP, Perl
15
XAMPP

16
PHP Language Basics
⚫ Look at the building blocks of the PHP
language
⚫ Syntax and structure
⚫ Variables and operators
⚫ Data types and conversions

17
PHP - Syntax and Structure
⚫ PHP is similar to C
⚫ All scripts start with <?php and end with ?>
⚫ Line separator: ; (semi-colon)
⚫ Code block: { //code here }
⚫ Comments are created using:
⚫ // single line quote
⚫ /* Multiple line
block quote */

18
PHP - Variables
⚫ Prefixed with a $
⚫ Assign values with = operator
⚫ Example: $author = “Trevor Adams”;
⚫ No need to define type
⚫ Variable names are case sensitive
⚫ $author and $Author are different

19
PHP - Example Script
<?php
$author = “Trevor Adams”;
echo $author;
$msg = “Hello world!”;
Echo $msg;
?>

20
PHP - Operators
⚫ Standard mathematical operators
⚫ +, -, *, / and % (modulus)
⚫ String concatenation with a period (.)
⚫ $car = “SEAT” . “ Altea”;
⚫ echo $car;
⚫ Would output “SEAT Altea”

21
String concatenation Example
<?php
$author = “Trevor Adams”;
$msg = “Hello world!”;
echo $author . “ says ” . $msg;
?>

22
PHP – Operators (cont.)
⚫ Basic Boolean comparison with “==”
⚫ Using only = will overwrite a variable value
⚫ Less than < and greater than >
⚫ <= and >= as above but include equality

23
PHP - Data Types
⚫ PHP is loosely typed language.

⚫ A data type is either text or numeric


⚫ PHP decides what type a variable is
⚫ PHP can use variables in an appropriate way automatically

⚫ E.g.
⚫ $vat_rate = 0.175; // vat rate is numeric
⚫ echo $vat_rate * 100 . “%”;
⚫ //outputs “17.5%”

24
PHP Data Types (cont.)

25
PHP Data Types (cont.)
⚫ We have to be careful and to test types during
execution
⚫ gettype() function returns a string representation of
variable’s type
$x = 5; echo gettype($x); // integer
$x = $x + 1.5; echo gettype($x) ; // float
$x = $x . “ dollars”; echo gettype($x); // string

⚫ is_<type>() function returns a boolean to test for a given


<type>
$x = 5; $check = is_int($x); // true
$check = is_float($x); // false
26
Conversion / Casting

▪ As PHP evaluates expressions, sometimes


values in the expression need to be
converted from one type to another as the
computations are done.
• PHP does aggressive implicit type conversion
(casting).

• You can also make type conversion (casting) explicit


with casting operators.
27
Conversion / Casting (cont.)
⚫ A special feature of PHP is that it attempts to
temporarily convert a variable to the
appropriate type for an operation.
⚫ By contrast, most compiled languages
usually allow operations to be performed only
on variables of the same type.
⚫ Consider the following PHP code:
⚫ $amount = 10;
⚫ $items = "5";
⚫ $total = $amount * $items;
28
Conversion / Casting (cont.)

⚫ Even though the code is multiplying an integer with a


string, the correct value of 50 is contained in $total.
⚫ Since the mathematical operators expect numbers as
operands, PHP tries to automatically convert the string to
a number before performing the multiplication.
⚫ If the string can’t be converted to a number, zero is used.
⚫ In the previse example, PHP converts $items to 5 and
performs the multiplication.

29
Casting In PHP, division forces
operands to be floating
point.
$a = 56; $b = 12;
$c = $a / $b;
echo "C: $c\n"; C: 4.66666666667
$d = "100" + 36.25 + TRUE; D: 137.25
echo "D: ". $d . "\n"; E: 8
$e = (int) 9.9 - 1; F: 25
echo "E: $e\n"; G: sam25
$f = "sam" + 25;
echo "F: $f\n";
$g = "sam" . 25;
echo "G: $g\n";

30
Q&A

31

You might also like