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

27 PHP Introduction

The document introduces PHP as a server-side scripting language used for web development. It discusses PHP syntax, variables, data types, expressions, and control flow structures like if/else statements. Key points covered include how PHP code is embedded in HTML pages, variable scoping and data type conversion, and using operators in expressions to control program logic.

Uploaded by

Ahsan Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

27 PHP Introduction

The document introduces PHP as a server-side scripting language used for web development. It discusses PHP syntax, variables, data types, expressions, and control flow structures like if/else statements. Key points covered include how PHP code is embedded in HTML pages, variable scoping and data type conversion, and using operators in expressions to control program logic.

Uploaded by

Ahsan Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

WEB

ENGINEERING
INTRODUCTION TO php
Engr. FARYAL BALOCH
Department of Software Engineering MUET
PHP (PHP Hypertext Preprocessor)
- Is a recursive acronym for PHP: Hypertext Preprocessor (earlier Personal Home Pages)
- PHP is an HTML-embedded Web scripting language.
- This means PHP code can be inserted into the HTML of a Web page.
- PHP scripts are executed on the server;
- what it means that PHP script runs entirely on server and only the results are sent to user’s
browser.
-What PHP is capable of?
- Generating dynamic page content
- Creating, opening, reading, writing, deleting, and closing files on the server
- Collecting form data
- Adding, deleting, and modifying information stored in your database.
- controlling user-access
- encrypting data
Why PHP?
- runs on numerous, varying platforms, including Windows, Linux, Unix, Mac OS X,
and so on.
- is compatible with almost any modern server, such as Apache, IIS, and more.
- supports a wide range of databases.
The Structure of PHP
◦ PHP Syntax:
- A PHP script starts with <?php and ends with?>
- entire sections of PHP can be placed inside this tag.
- Alternatively, we can include PHP in the HTML <script> tag.
The Structure of PHP…
◦ PHP Statements
- Each PHP statement must end with a semicolon.

- forgetting a semicolon would trigger a Parse error message.


The Structure of PHP…
◦ PHP Case Sensitivity:
- In PHP, keywords (e.g., if, else, while, echo, etc.), classes, functions, and
user-defined functions are not case-sensitive.
- However, all variable names are case-sensitive! ($name and $NAME would be two
different variables)
The Structure of PHP…
◦ PHP Comments:

COMMENTS

single line
multiline
/* This is a section
// This is a comment
multiline comments*/
The Structure of PHP…
◦ PHP Variables:
- Variables are used as "containers" in which we store information.
- You must place a $ in front of all variables.
- Unlike other programming languages, PHP has no command for declaring a
variable.
- PHP automatically converts the variable to the correct data type, depending on its
value.
The Structure of PHP…
Rules for creating PHP variables:
▪ Must start with $ sign.
▪ variable name must start with a letter or an underscore.
▪ variable name cannot start with a number.
▪ A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _)
▪ Strings must be enclosed in quotation marks.
The Structure of PHP…
◦ PHP Variable Scope:
▪ PHP variables can be declared anywhere in the script.
▪ Scope refers to the visibility of variables; in other words, which parts of your
program can see or use it.
▪ Normally, every variable has a global scope.

- Global Variables:
▪ A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function.
The Structure of PHP…

- This script will produce an error, as the $name variable has a global scope, and is
not accessible within the getName() function.
The Structure of PHP…
- How to solve this issue? The global keyword is used to access a global
variable from within a function.
The Structure of PHP…
- Local Variables:
▪ Whereas local variables are created within, and can be accessed only by, a function.

- The static keyword:


▪ Normally, when a function is completed/executed, all its variables are deleted.
▪ However, sometimes we want a local variable NOT to be deleted.
▪ We need it for a further job.
▪ To do this, use the static keyword when you first declare the variable:
The Structure of PHP…
◦ Variable Variables
▪ With PHP, you can use one variable to specify another variable's name.
▪ So, a variable treats the value of another variable as its name.
The Structure of PHP…
◦ PHP Constants
▪ once you have defined a constant, its value is set for the remainder of the program
and cannot be altered.
▪ constants are automatically global across the entire script.
▪ A valid constant name starts with a letter or underscore (no $ sign before the constant
name).
▪ To create a constant, use the define() function.
The Structure of PHP…
◦ PHP Constants
- Where:
▪ 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.
The Structure of PHP…
◦ PHP Data Types:
- PHP supports these data types: String, Integer, Float, Boolean, Array, Object,
NULL.
◦ Strings:
- A string is a sequence of characters, like "Hello world!"
- A string can be any text within a set of single or double quotes.
The Structure of PHP…
◦ PHP Data Types:
- If you wish to assign a literal string, preserving the exact contents, you should use
the single quotation mark (apostrophe), like this:

- If you had used double quotes, PHP would have attempted to evaluate $variable as a
variable.
The Structure of PHP…
◦ PHP Data Types:
◦ Integers:
- An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647
The Structure of PHP…
◦ PHP Data Types:
◦ Float:
- A float, or floating-point number, is a number that includes a decimal point.
The Structure of PHP…
◦ PHP Data Types:
◦ Boolean:
- A Boolean represents two possible states: TRUE or FALSE.
The Structure of PHP…
◦ PHP Data Types:
- NOTE: Most of the data types can be used in combination with one another.
PHP Expressions
- An expression is a combination of values, variables, operators, and functions that
results in a value; the result can be a number, a string, or a Boolean value.
- Almost everything in a PHP script is an expression.
- Anything that has a value is an expression.

- Literals and Variables: The simplest form of an expression is a literal, which


means something that evaluates to itself.
- An expression could also be a variable, which evaluates to the value that has been
assigned to it.
- They are both types of expressions, because they return a value.
PHP Expressions…
PHP – Expressions…
- PHP Statements: When you combine assignment or control-flow constructs with
expressions, the result is a statement.

- The first assigns the result of the expression 366 - day_number to the variable
days_to_new_year, and the second outputs a friendly message only if the expression
days_to_new_year < 30 evaluates to true.
JavaScript – Expressions…
- PHP Operators: Operators let you create more-complex expressions that evaluate
to useful results.
- PHP offers a lot of powerful operators that range from arithmetic, string, and logical
operators to assignment, comparison, and more.
JavaScript – Expressions…
- PHP Operator Precedence :
PHP – Expressions…
- PHP Associativity: Most PHP operators are processed in order from left to right in
an equation.
- But some operators require processing from right to left instead.
- The direction of processing is called the operator’s associativity.
- Look at the following assignment operators, by which three variables are all set to the
value 0:

- This multiple assignment is possible only because the rightmost part of the expression
is evaluated first and then processing continues in a right-to-left direction.
PHP – Expressions…
- PHP Relational Operators: Relational operators test two operands and return a
Boolean result.
- There are three types of relational operators: equality, comparison, and logical.
- The equality operator is == (which should not be confused with the = assignment
operator).

- the first statement assigns a value, and the second tests it for equality.
- As it stands, “its springtime” will be printed out, because month is assigned the string
value March, and therefore the check for it having a value of March will succeed.
PHP – Expressions…
- The equality operator: If the two operands of an equality expression are of different types,
PHP will convert them to whatever type makes best sense to it.
- For example, any strings composed entirely of numbers will be converted to numbers
whenever compared with a number.

- if you run the example, you will see that it outputs the number 1, which means that the first if
statement evaluated to true. This is because the string value of b was first temporarily
converted to a number, and therefore both halves of the equation had a string value
PHP – Expressions…
- PHP Comparison Operators: Using comparison operators, you can test for more
than just equality and inequality.
- PHP also gives you > (is greater than), < (is less than), >= (is greater than or equal to),
- and <= (is less than or equal to) to play with.
PHP – Expressions…
- PHP Logical Operators: Logical operators produce true-or-false results, and
therefore are also known as Boolean operators. There are four of them
PHP – Control Flow
- Conditionals alter program flow.
- They enable you to ask questions about certain things and respond to the answers you
get in different ways. There are three types of non-looping conditionals: the if
statement, the switch statement, and the ? operator.
- If statement: The if statement is used to execute a certain code if a condition is true.
- NOTE: Contents of the if condition can be any valid PHP expression, including
equality, comparison, tests for 0 and NULL, and even the values returned by
functions.
- The actions to take when an if condition is TRUE are generally placed inside curly
braces, { }.
PHP – Control Flow…
▪ PHP If…. Else Statement:
- With an if...else statement, the first conditional statement is executed if the condition
is TRUE.
- But if it is FALSE, the second one is executed.
- One of the two choices must be executed.
PHP – Control Flow…
PHP – Control Flow…
▪ PHP - The if...elseif...else Statement:
- executes different codes for more than two conditions.
PHP – Control Flow…
PHP – Control Flow…
▪ Switch Statement:
- The switch statement is useful in cases in which one variable, or the result of an
expression can have multiple values, which should each trigger a different function.
PHP – Control Flow…
▪ Switch Statement:
- NOTE: Failing to specify the break statement causes PHP to continue executing the statements
that follow the case, until it finds a break.
- You can use this behavior if you need to arrive at the same output for more than one case.
PHP – Control Flow…
▪ The ? operator:
- One way of avoiding the verbosity of if and else statements is to use the more
compact ternary operator, ?, which is unusual in that it takes three operands rather
than the typical two.

- Assigning a ? conditional result to a variable


THANK YOU

You might also like