Docsity

PHP: Introduction to PHP Programming, Slides of Web Programming and Technologies

Partial preview of the text

Download PHP: Introduction to PHP Programming and more Slides Web Programming and Technologies in PDF only on Docsity!

PHP

Lecture 01

PHP

  • (^) PHP is an acronym for "PHP: Hypertext Preprocessor“.
  • (^) PHP is a widely-used, open source scripting language.
  • (^) PHP is a server scripting language, and a powerful tool

for making dynamic and interactive Web pages.

  • (^) PHP files can contain text, HTML, CSS, JavaScript, and

PHP code.

  • (^) PHP code are executed on the server, and the result is

returned to the browser as plain HTML.

  • (^) PHP file has extension ".php“.

XAMPP

PHP Basic Syntax

  • (^) A PHP script can be placed anywhere in the document.
  • (^) A PHP script starts with ****

The default file extension for PHP files is “.php”

  • (^) PHP statements end with a semicolon (;).

Welcome to PHP

PHP Case Sensitivity

In PHP, all keywords (e.g. if, else, while, echo,

etc.), classes, functions, and user-defined

functions are NOT case-sensitive.

";

echo "Hello World!";

EcHo "Hello World!";

However; all variable names are case-sensitive.

$name and $Name are two different variables

Variables

In PHP, a variable starts with the $ sign, followed by

the name of the variable:

  • (^) PHP is a Loosely Typed Language; means we don’t

have to specify datatype with variable. PHP

automatically converts the variable to the correct data

type, depending on its value.

Rules for PHP variables:

 A variable starts with the $ sign, followed by the name of

the variable

 A variable name must start with a letter or the underscore

character

 A variable name cannot start with a number

 A variable name can only contain alpha-numeric

characters and underscores (A-z, 0-9, and _ )

 Variable names are case-sensitive ($age and $AGE are

two different variables)

For example:

Output Statement

  • (^) In PHP there are two basic ways to get output: echo and print.
  • (^) Echo and print
  • (^) echo can take multiple parameters.
  • print takes only one argument.

";

$name = "Ali ";

echo $name;

print "PHP";

echo "I ", "am ", "in ", "5th ", "semester. ";

//print "I ", "am ", "in ", "5th ", "semester"; This is not allowed

?>

Data Types

PHP supports the following data types:

  • (^) Integer

Float (floating point numbers - also called double)

  • (^) Boolean
  • (^) String

Array

  • (^) Object

NULL

  • (^) Resource

Float

A float (floating point number) is a number with a

decimal point or a number in exponential form.

php

$x = 25e-2; //25*

$y = 2.5;

var_dump($x);

//echo $y;

Boolean

A Boolean represents two possible states: TRUE

or FALSE.

  • (^) Booleans are often used in conditional testing.

$x = true;

$y = false;

String Functions

There are lots of string functions available in PHP. Some of

these are presented here;

Length of a String

  • (^) strlen() function returns the length of a string. It actually

returns the number of bytes in a string.

echo strlen("Web"); // Outputs 3

Count The Number of Words in a String

  • (^) str_word_count() function counts the number of words in a

string:

echo str_word_count("Web Engineering"); // outputs 2

Reverse a String

  • (^) strrev() function reverses a string:

echo strrev("Web"); // output is beW

String Functions (Cont…)

Replace Text Within a String

  • (^) str_replace() function replaces some characters

with some other characters in a string.

echo str_replace("web", "z", "PHP is a web

scripting language"); // Output is PHP is a z

scrtipting language

Constants

A constant is an identifier (name) for a simple value.

The value cannot be changed during the script.

  • (^) A valid constant name starts with a letter or

underscore (no $ sign before the constant name).

  • (^) Note: Unlike variables, constants are automatically

global across the entire script.

  • (^) Syntax

define( name , value , case-insensitive )

name : Specifies the name of the constant

value : Specifies the value of the constant

case-insensitive : Specifies whether the constant

name should be case-insensitive. Default is false.

Constant (Cont…)

The example below creates a constant with

a case-sensitive name:

".MSG;