Variables - LEARNING PHP AND MYSQL (2015)

LEARNING PHP AND MYSQL (2015)

Variables

Variables are used to store values like strings numbers etc. When a variable is declared, it can be used again and again in your script. Symbol used to represent PHP variables is “$ “.

Syntax

$ var _ name = value;

There is no need to specify in PHP that the variable use belong to which data type. PHP converts the variable into correct data type. A variable name can also contain alpha numerical characters but it should not contain space.

Example

<?php

$name = ‘Raj’;

$year born = 1977;

$current year = 2015;

$age = $current year - $year born;

Print (“$name is $age years old”)

?>

Predefined variables

The work environment that is been used by you is been accessed by the PHP script. Such variables are stored in a special hash data like $_ENV and $_SERVER. These variables can be used only one two in count at a time.

Example

<HTML>

<HEAD><TITLE>Example 2</TITLE> </HEAD>

<BODY>

<?

print(“hello>”);

print(“you are using $_SERVER[HTTP_SERVER_AGENT]<br>”);

print(“your internet address is $_SERVER[REMOTE_ADDR]<br>”);

?>

</BODY>

</HTML>

Variable from external source

Html forms (GET and POST)

Information from the form when submitted to PHP is automatically made available to the particular script.

Example

<form action = “foo.php” method = “post”>

Name- <input type = “text” name = “username” /><br />

Email- <input type = “text” name= “email” /><br/>

<input type = “submit” name = “submit” value = “submit me!” />

</form>

Arrays

Array is an ordered map actually in PHP .This type of optimization is so that it can have several different uses. Arrays value can be arrays, trees and even multidimensional arrays.

Example

Array (

Key => value,

Key a => value2,

Key b => value3,

….

)

Array can also be made using a key/value pair combination. These are also considered as associative arrays and hashes. An easy way to compress hash work is to think it as a table.

Example

Country (Key)

City (Value)

Cambodia

Phnom Penh

Lebanon

Beirut

Cuba

Habana

Indonesia

Jakarta

Finland

Helsinki

Table

<? php // create the array (

‘Cambodia’ => ‘Phom Penh’,

‘Lebanon’ => ‘Beirut’,

‘Cuba’ => ‘Habana’,

‘Indonesia =>’Jakarta’,

‘Finland’ => ‘Helsinki’);

Print ($capital [‘Cambodia’]);

?>

Predefined constant of array

· CASE_LOWER

· CASE_UPPER

· SORT_ASC

· SORT_DESC

· COUNT_NORMAL

· COUNT_RECURSIVE

· EXTR_SKIP

· EXTR_OVERWRITE

· EXTR_IF_EXISTS.