1. PHP Introduction

What is PHP ?
PHP: hypertext preprocessor ( personal home page )
  • It is a interpreted language i.e no need of compilation.
  • It is also called as server side sripting language.
  • A widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

2. Creating a first PHP Page


=> This special markup indicates that we are about to start page using PHP.
=> It starts a PHP section as far as the PHP engine on the server is conneced.

WHAT HAPPENS:- When the server sends a PHP pages back to the browser, the PHP engine starts by opening that page. When it sees the markup <?php, it begins interpreting what follows as PHP.At the end of PHP page, we use the closing markup, which is ?>.

3. 'Hello World' program by PHP

EXAMPLE:

RESULT:


4. Features of PHP

  • PHP can generate dynamic page content.
  • PHP can create, open, read, write, delete and close files on the server.
  • PHP can collect form data.
  • PHP can sends and receives cookies.
  • PHP can add, delte, modify data in your database.
  • PHP can be used to control user-access.
  • PHP can encrypt data.
  • PHP are case-sensitve.

5. Display PHP variables

EXAMPLE:

RESULT:


6. PHP Data Types

PHP supports the following data types:-
  1. String
  2. Integer
  3. Float (double)
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resources

PHP String

A string can be any text inside quotes. We can use single or double quotes.

EXAMPLE:

RESULT:


PHP Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
EXAMPLE:

RESULT:


PHP float

A float (floating point number) is a number with a decimal point or a number in exponential form.

EXAMPLE:

RESULT:


PHP boolean

A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing.

EXAMPLE:


PHP Array

An array stores multiple values in one single variable.

EXAMPLE:

RESULT:


PHP Object

An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:

EXAMPLE:

RESULT:


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:

EXAMPLE:

RESULT:


PHP Resources

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.

7. PHP Constant

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.

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:

RESULT:


8. PHP String

A string is a sequence of characters, like "Hello world!".

Get the length of a String

The PHP strlen() function returns the length of a string.

EXAMPLE:

RESULT:


Count The Number of Words in a String

The PHP str_word_count() function counts the number of words in a string:

EXAMPLE:

RESULT:


Reverse a String

The PHP strrev() function reverses a string:

EXAMPLE:

RESULT:


Search For a Specific Text Within a String

The PHP strpos() function searches for a specific text within a string.

If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

EXAMPLE:

RESULT:


Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.

EXAMPLE:

RESULT:


9. PHP Loop

Often when we write code, we want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this.

In PHP, we have the following looping statements:

  • 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

PHP while Loop

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

EXAMPLE:

RESULT:


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.

EXAMPLE:

RESULT:


PHP for Loop

PHP for loops execute a block of code a specified number of times.

EXAMPLE:

RESULT:


PHP foreach Loop

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

EXAMPLE:

RESULT:


10. PHP Arrays

An array stores multiple values in one single variable:

What is an Array ?

An array is a special variable, which can hold more than one value at a time.

Create an Array in PHP

In PHP, the array() function is used to create an array:

In PHP, there are three types of arrays:

  • Indexed Arrays-Arrays with a numeric index.
  • Associative Arrays-Arrays with named keys.
  • Multidimensional Arrays-Arrays containing one or more arrays.

PHP Indexed Array

There are two ways to create indexed arrays:

The index can be assigned automatically (index always starts at 0).

or the index can be assigned manually:

The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:

EXAMPLE:

RESULT:


Get The Length of an Array - The count() Function

The count() function is used to return the length (the number of elements) of an array:

EXAMPLE:

RESULT:


Loop Through an Indexed Array

To loop through and print all the values of an indexed array, you could use a for loop, like this:

EXAMPLE:

RESULT:


PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array:

or the index can be assigned manually:


EXAMPLE:

RESULT:


Loop through an Associative arrays

To loop through and print all the values of an associative array, you could use a foreach loop, like this:

EXAMPLE:

RESULT:


Multidimensional Array

A multidimensional array is an array containing one or more arrays.