Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 79

Web Development

Introduction to PHP
SEM-4
What is PHP?
• PHP stands for Hypertext Preprocessor
• It’s a general purpose scripting language that is freely available
and widely used for web development.
• Server side scripting language.
• With PHP we create dynamic websites.
• Latest vesion of PHP 8.1
• Extension .php
What is PHP?
• PHP -> HTML ------ CSS-------Java Script

• Apache( webserver) waiter

• PHP cook or chef

• MySQL(Database)storageroom
PHP Syntax
• <? PHP

• ?>
XAMPP INSTALLATION
• Always save file with .php

• We can print anything in PHP by

1. Echo not case sensitive


2. Print_r()
Comments
• // Variable –single line comments
• # Varible – single line comments

• /* */ -multiple line comments


Variable
• Variable used for store any value and string
• Syntax for variable
$name

Variable start with letter or _

It is a case sensitive
• PHP variables can store various types of data, such as strings,
numbers, arrays, objects, etc. The type of data a variable holds is
determined dynamically based on the value assigned to it.
• Here are a few examples with different data types:
In this example:
1.We declare and assign values to two variables, $greeting and $name.
2.We concatenate these variables to create a new variable called $message.
3.Finally, we use echo to output the value of $message.
• When you run this PHP code, it will output:
•Variable Types
• PHP has no command for declaring a variable, and the data type depends on the
value of the variable.
• PHP supports the following data types:
• Integer
• Float (floating point numbers also called double)
• Boolean
• Resource
• Array
• String
• Object
• Null
PHP Variables Scope(types)
• In PHP, variables can be declared anywhere in the script.

• The scope of a variable is the part of the script where the variable can be
referenced/used.

• PHP has three different variable scopes:

1. Local
2. Global
3. static
Global and Local Scope
• A variable declared outside a function has a GLOBAL SCOPE and can
only be accessed outside a function:

• A variable declared within a function has a LOCAL SCOPE and can only
be accessed within that function:
PHP Static
• Normally, when a function is completed/executed, all of 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:
Output
PHP String Output
PHP Float
PHP Boolean
PHP Array
• An array stores multiple value in one single variable.
• In the following example $cars is an array. The PHP var_dump()
function returns the data type and value:
Output
• PHP Object
• Classes and objects are the two main aspects of object-oriented
programming
• A class is a template for objects, and an object is an instance of a
class.
• When the individual objects are created, they inherit all the
properties and behaviors from the class, but each object will have
different values for the properties.
• Let's assume we have a class named Car that can have properties like
model, color, etc. We can define variables like $model. $color, and so
on, to hold the values of these properties.
• When the individual objects (Volvo, BMW, Toyota, etc.) are created,
they inherit all the properties and behaviors from the class, but each
object will have different values for the properties.
PHP Null
• Null is a special data type which can have only one value: NULL.

• A variable of data type NULL is a variable that has no value assigned


to it.

• Tip: If a variable is created without a value, it is automatically assigned


a value of NULL.

• Variables can also be emptied by setting the value to NULL:


PHP Resource
• The special resource type is not an actual data type. It is the storing of
a reference to functions and resources external to PHP.

• A common example of using the resource data type is a database call.

• We will not talk about the resource type here, since it is an advanced
topic.
PHP String
• Strings in PHP are surrounded by either double quotation marks, or single quotation marks.

• Double or Single Quotes?

• You can use double or single quotes, but you should be aware of the differences between
the two.

• Double quoted strings perform action on special characters. E.g. when there is a variable in
the string, it returns the value of the variable.

• Single quoted strings does not perform such actions, it returns the string like it was written,
with the variable name:
PHP Constants:- Constants are like variables, except that
once they are defined they cannot be changed or undefined.
• 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.

• Create a PHP Constant

• To create a constant, use the define() function.


Syntax
• define(name, value, case-insenstive);

• 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
Examples

Case senstive Case insensitive


PHP Const Keyword
Const vs. define
• const are always case-sensitive
• define() has has a case-insensitive option.

• const cannot be created inside another block scope like inside a


function or Inside an if statement.

• define can be created inside another block scope.


PHP Slicing
• Slicing You can return a range of characters by using the substr()
function. Specify the start index and the number of characters you
want to return.
PHP Escape character
• To Insert characters that are illegal in a string, use an escape
character.

• An escape character is a backslash followed by the character you want


to insert.

• An example of an illegal character is a double quote inside a string


that is surrounded by double quotes
PHP MATH

PI FUNCTION PHP MIN AND MAX FUNCTION


PHP ABSOLUTE NUMBER PHP SQRT
PHP ROUND NUMBER PHP RANDOM NUMBER
PHP OPERATORS
• Operators are used to perform operations on variables and values.

• PHP divides the operators in the following groups:


• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
Arithmatic operators
• The PHP arithmetic operators are used with numeric values to
perform common arithmetical operations, such as addition,
subtraction, multiplication etc.
PHP Assignment Operators
• The PHP assignment operators are used with numeric values to write
a value to a variable.
• The basic assignment operator in PHP is”=”.It means that the left
operand gets set to the value of the assignment expression on the
right.
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or
string):
PHP Increment / Decrement Operators
• The PHP increment operators are used to increment a variable's
value.

• The PHP decrement operators are used to decrement a variable's


value.
PHP Logical Operators
• The PHP logical operators are used to combine conditional
statements.
Array union operation
PHP Switch statement
PHP Array Operators
PHP Loops
• Often when you write code, you want the same block of code to run over and
over again a certain number of times. So, instead of adding several almost
equal code-lines in a script, we can use loops.
• Loops are used to execute the same block of code again and again, as long as a
certain condition is true.
• In PHP, we have the following loop types:
1. while-loops through a block of code as long as the specified condition is true
2. do. while loops through a block of code once, and then repeats the loop as
long as the specified condition is true
3. for-loops through a block of code a specified number of times
4. foreach loops through a block of code for each element in an array
The PHP while Loop
• The while loop executes a block of code as long as the specified
condition is true.
Do while

You might also like