You are on page 1of 21

Server-side scripting :

In client server programming , the Web servers are used to execute server-side scripting. They are
basically used to create dynamic pages. It can also access the file system residing at the webserver. A
server-side environment that runs on a scripting language is a web server.

Scripts can be written in any out of a number of server-side scripting languages available. It is
used to retrieve and generate content for dynamic pages. When we need to store and retrieve information a
database will be used to contain data known as Database Server. Theserver sends pages to the request of the
user/client. Various popular server side scripts are: PHP, Python, JavaScript, JSP, Ruby, Perl etc.

Basics of PHP

PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995. PHP 7.4.0is the latest
version of PHP, which was released on 28 November 2019.

PHP stands for Hypertext Preprocessor. It is an open-source, interpreted, and object-orientedscripting


language that can be executed at the server-side. PHP is well suited for web development. Therefore, it is
used to develop web applications (an application that executes on the server and generates the dynamic
page.).

Advantages

1. PHP is an interpreted language, i.e., there is no need for compilation.

2. PHP is faster than other scripting languages, for example, ASP and JSP.

3. PHP is a server-side scripting language, which is used to manage the dynamic content of thewebsite.

4. PHP can be embedded into HTML.

5. PHP is an object-oriented language.

6. PHP is an open-source scripting language.

7. PHP is simple and easy to learn language.

8. It handles dynamic content, database as well as session tracking for the website.

9. can create sessions in PHP.

10. It can access cookies variable and also set cookies.

11. It helps to encrypt the data and apply validation.

12. PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.
Features of PHP

PHP is very popular language because of its simplicity and open source. There are someimportant features
of PHP given below:

1. Performance:

PHP script is executed much faster than those scripts which are written in other languages such as JSP and
ASP. PHP uses its own memory, so the server workload and loading time isautomatically reduced, which
results in faster processing speed and better performance.

2. Open Source:

PHP source code and software are freely available on the web. We can develop all the versions of PHP
according to our requirement without paying any cost. All its components are free to download and use.

3. Familiarity with syntax:

PHP has easily understandable syntax. Programmers are comfortable coding with it.

4. Embedded:

PHP code can be easily embedded within mark up languanges like HTML tags and script.

5. Platform Independent:

PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP applicationdeveloped
in one OS can be easily executed in other OS also.

6. Database Support:

PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.

7. Error Reporting -

PHP has predefined error reporting constants to generate an error notice or warning at runtime. E.g.,
E_ERROR, E_WARNING, E_STRICT, E_PARSE.

8 Loosely Typed Language:

PHP allows us to use a variable without declaring its datatype. It will be taken automatically atthe time of
execution based on the type of data it contains on its value.

9. Web servers Support:

PHP is compatible with almost all local servers used today like Apache, Netscape, Microsoft IIS, etc.
10. Security:

PHP is a secure language to develop the website. It consists of multiple layers of security toprevent threads
and malicious attacks.

11. Control:

Variables in PHP

In PHP, a variable is declared using a $ sign followed by the variable name. Properties of PHP

variables are:

1. As PHP is a loosely typed language, so we do not need to declare the data types of the variables. It
automatically analyzes the values and makes conversions to its correct datatype.

2. After declaring a variable, it can be reused throughout the code.

3. Assignment Operator (=) is used to assign the value to a variable.

4. A variable must start with a dollar ($) sign, followed by the variable name.

5. Character Set: It can only contain alpha-numeric character and underscore (A-z, 0-9, _).

6. A variable name must start with a letter or underscore (_) character.

7. A PHP variable name cannot contain spaces.

One thing to be kept in mind that the variable name cannot start with a number or specialsymbols.

PHP variables are case-sensitive, so $name and $NAME both are treated as different variable. Syntax

$variablename=value; Example:

$str="Good Morning";

$x=200;

$y=44.6;
Data Types

Data Types define the type of data a variable can store. PHP allows eight different types of data types. These
eight types are categorised under three types.

1. Scalar Types (predefined)

Boolean

Integer

Double

String

2. Compound Types (user-defined)

Array

Objects

3. Special Types

NULL

resource

Out of these eight datatupe , the first five are called simple data types and the last three arecompound data
types:

1. Integer : Integers hold only whole numbers including positive and negative numbers, i.e., numbers
without fractional part or decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal
(base 16). The default base is decimal (base 10). The octal integers can be declared with prefix 0 and the
hexadecimal can be declared with prefix 0x. The range of integers must lie between -2^31 to 2^31.

Example:

Decimal base or default base:

$num1 = 50;

$num2 = 654;

octal base integers:

$val1 = 07;

hexadecimal base integers :

$val2 = 0x45;
2. Double:It Can hold numbers containing fractional or decimal parts of positive and negative numbers
or a number in exponential form. By default, the variables add a minimum number of decimal places.
The Double data type is the same as a float as floating-point numbers or real numbers.

Example:

$val1 = 50.85;

$val2 = 654.26;

3. String: It Hold letters or any alphabets, evenif numbers are also included. These are written within
double quotes during declaration. The strings can also be written within single quotes, but they will be
treated differently while printing variables. Example:

Example:

$name = "Rama";

4. NULL: These are special types of variables that can hold only one value i.e., NULL. We follow the
convention of writing it in capital form, but it’s case-sensitive. If a variable is created withouta value or no
value, it is automatically assigned a value of NULL. It is written in capital letters.

Example:

$num = NULL;

5. Boolean: Boolean data types hold only two values, either TRUE(1) or FALSE(0) and used for
Boolean conditions. Successful events will return true and unsuccessful events return false. NULL type
values are also treated as false in Boolean. Apart from NULL, 0 is also considered false in boolean. If a
string is empty then it is also considered false in boolean data type.

Example:

if(TRUE)

echo "Rama";

6. Arrays: Array is a compound data-type that can store multiple values of the same data type. Below is
an example of an array of integers.

Example:

$a = array( 10, 20 , 30);


echo " $a[0]";

7. Objects: Objects are defined as instances of user-defined classes that can hold both values or data and
functions or methods . When the objects are created, they inherit all the properties and behaviors from the
class, having different values for all the properties.

Objects are created by the new keyword.

8. Resources: Resources in PHP are not an exact data type. These are basically used to store references to
some function call or to external PHP resources. For example, Resource variables holds references to files
and database connections.
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-insensitive)

Parameters:

 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

Example
Create a constant with a case-insensitive name:

Conditional Statements
Conditional statements are used to perform different actions based on different conditions.

Very often when you write code, you want to perform different actions for different conditions. You can
use conditional statements in your code to do this.

In PHP we have the following conditional statements:

 if statement - executes some code if one condition is true


 if...else statement - executes some code if a condition is true and another code if that
condition is false
 if...elseif...else statement - executes different codes for more than two conditions
 switch statement - selects one of many blocks of code to be executed
PHP - The if Statement
The if statement executes some code if one condition is true.

Syntax
if (condition) {
code to be executed if condition is true;
}

Example
Output "Have a good day!" if the current time (HOUR) is less than 20:

PHP - The if...else Statement


The if...else statement executes some code if a condition is true and another code if that condition is
false.

Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Example
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
PHP - The if...elseif...else Statement
The if...elseif...else statement executes different codes for more than two conditions.

Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}

Example
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current
time is less than 20. Otherwise it will output "Have a good night!":

The PHP switch Statement


Use the switch statement to select one of many blocks of code to be executed.

Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

This is how it works: First we have a single expression n (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each case in the structure. If there is a
match, the block of code associated with that case is executed. Use break to prevent the code from
running into the next case automatically. The default statement is used if no match is found.
Example

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:

 while - loops through a block of code as long as the specified condition is true
 do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
 for - loops through a block of code a specified number of times
 foreach - loops through a block of code for each element in an array

The following chapters will explain and give examples of each loop type.

The PHP while Loop


The while loop executes a block of code as long as the specified condition is true.

Syntax
while (condition is true) {
code to be executed;
}

Examples
The example below displays the numbers from 1 to 5:
The PHP do...while Loop
The do...while loop will always execute the block of code once, it will then check the condition, and
repeat the loop while the specified condition is true.

Syntax
do {
code to be executed;
} while (condition is true);

Examples
The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output,
and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to
5?), and the loop will continue to run as long as $x is less than, or equal to 5:

The PHP for Loop


The for loop is used when you know in advance how many times the script should run.

Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}

Parameters:

 init counter: Initialize the loop counter value


 test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it
evaluates to FALSE, the loop ends.
 increment counter: Increases the loop counter value

Examples
The example below displays the numbers from 0 to 10:

The PHP foreach Loop


The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Syntax
foreach ($array as $value) {
code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value and the array
pointer is moved by one, until it reaches the last array element.

Examples
The following example will output the values of the given array ($colors):

You might also like