Professional Documents
Culture Documents
Table of Content
‣ AMP Package
‣ Type Casting
‣ Overview of PHP
‣ PHP Operators
‣ PHP - Environment Setup
‣ Conditional Statements
‣ PHP with HTML
‣ PHP Loops
‣ PHP Syntax
‣ PHP Arrays
‣ Output/Printing in PHP
‣ PHP Functions
‣ PHP Variables
‣ Date and Time Function
‣ PHP Data Types
‣ Global Variables - Superglobals
‣ PHP Strings
‣ File Handling
‣ PHP - Constants
Prof. Jayakumar Sadhasivam, VIT, Vellore. 2
AMP Package
๏ AMP is an acronym formed from the initials of Apache,
MySQL and PHP or Python or Perl.
✓ Apache: It acts as Web server. Its main job is to parse any
file requested by a browser and display the correct results
according to the code within that file.
✓ PHP: PHP is a server-side scripting language that allows
your Web site to be truly dynamic.
✓ MySQL: It enables PHP and Apache to work together to
access and display data in a readable format to a browser.
Apache Introduction
๏ Apache is a Web server Responds to client requests by providing
resources
๏ The first version of Apache, based on the NCSA(National Center
for Supercomputing Applications) https Web server, was
developed in 1995.
๏ Because it was developed from existing NCSA code plus various
patches, It was called a patchy server - hence the name Apache
Server.
๏ As a result of its sophisticated features, excellent performance,
and low price - free, Apache has become the world's most popular
Web server.
What is PHP?
๏ PHP is a widely-used, open source scripting language
๏ PHP is free to download and use
๏ PHP scripts are executed on the server
✓ Capable of generating the HTML pages
๏ HTML generates the web page with the static text and images.
https://w3techs.com/technologies/overview/programming_language
PHP Syntax
๏ PHP is denoted in the page with opening and closing tags as
follows:
Output/Printing in PHP
๏ echo & print() is the common method in outputting data.
๏ Since it is a language construct, echo doesn’t require parenthesis
like print().
<?php
echo "Hello World";
print("Hello World");
?>
Output:
Hello World
Hello World
Comments in PHP
<?php
// This is a single-line comment
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
PHP Variables
๏ Variables are "containers" for storing information.
๏ Rules for PHP variables:
✓ A variable starts with the $ sign, followed by the name of the
variable
✓ A variable name must start with a letter or the underscore
character
✓ A variable name cannot start with a number
✓ A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
✓ Variable names are case-sensitive
‣ $age and $AGE are two different variables)
Variables
VALID INVALID
$prod_desc $9OctSales
$Intvar Sales123
$_Salesamt $*asgs
PHP Facts
๏ PHP is a Loosely Typed Language
✓ PHP automatically associates a data type to the variable,
depending on its value.
✓ Since the data types are not set in a strict sense, you can do
things like adding a string to an integer without causing an
error.
PHP Integer
๏ An integer data type is a non-decimal number between -2,147,483,648
and 2,147,483,647.
๏ An integer can be either positive or negative.
<?php
$int_var = 12345;
$another_int = -123456 + 12345;
echo $int_var;
echo $another_int;
?>
12345
-111111
Prof. Jayakumar Sadhasivam, VIT, Vellore. 28
PHP Float
๏ A float (floating point number) is a number with a decimal point
or a number in exponential form.
<?php
$many = 2.28888002;
$many_2 = 2.2111200;
$few = $many + $many_2;
echo "$many + $many_2 = $few";
?>
PHP Boolean
๏A Boolean represents two possible states: TRUE or
FALSE.
<?php
$x = true;
$y = false;
?>
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.
๏ 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:
<?
$my_var = NULL;
$mmy_var = null;
?>
Prof. Jayakumar Sadhasivam, VIT, Vellore. 31
PHP Array
๏ An array stores multiple values in one single variable.
<?
$cars = array("Volvo","BMW","Toyota");
echo $cars[0];
echo $cars[1];
echo $cars[2];
?>
PHP Strings
PHP Strings
๏ A string is a sequence of characters, like "Hello world!”.
<?php
$string_1 = "This is a string in double quotes";
$string_2 = 'This is a somewhat longer, singly
quoted string’;
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
echo $string_1;
echo $string_2; Output:
This is a string in double quotes
echo $string_39;
This is a somewhat longer, singly
echo $string_0; quoted string
?> This string has thirty-nine characters
--------------------- ---------------------
Output: Output:
$name is $age Jay is 23
Prof. Jayakumar Sadhasivam, VIT, Vellore. 35
String Functions
๏ strlen() - Return the Length of a String
<?php
echo strlen("Hello world!");
// outputs 12
?>
String Functions
๏ strpos() - Search For a 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.
<?php
echo strpos("Hello world!", "world");
// outputs 6
?>
String Functions
๏ str_replace() - Replace Text Within a String
✓ str_replace() function replaces some characters with some
other characters in a string.
<?php
$para = "Its PHP world!!!, welcome to Hello world !!";
$newPara = str_replace("world", "Server", $para);
echo $newPara;
?>
PHP - Constants
PHP - Constants Types
๏ 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).
๏ To create a constant, use the define() function.
๏ Syntax
define(name, value, case-insensitive)
Parameters: Optional
๏ 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
✓ settype()
✓ gettype()
<?php
$pi = 3.14;
settype($pi,"string");
echo $pi;
settype($pi,"integer"); Output:
3.14
echo $pi;
3
?>
Prof. Jayakumar Sadhasivam, VIT, Vellore. 47
PHP Operators
PHP Operators
๏ Operators are used to perform operations on variables and values.
๏ PHP language supports following type of operators.
✓ Arithmetic operators
✓ Assignment operators
✓ Comparison operators
✓ Increment/Decrement operators
✓ Logical operators
✓ String operators
✓ Array operators
✓ Conditional assignment operators
Prof. Jayakumar Sadhasivam, VIT, Vellore. 50
Assignment Operators
Logical Operators
Operator Name Description Result
<?php <?php
$txt1 = "Hello"; $txt1 = "Hello";
$txt2 = " world!"; $txt2 = " world!";
echo $txt1 . $txt2; $txt1 .= $txt2;
?> echo $txt1;
Output: ?>
Hello world! Output:
Hello world!
Prof. Jayakumar Sadhasivam, VIT, Vellore. 56
Array Operators
Conditional
Statements
Conditional Statements
๏ Conditional statements are used to perform different actions
based on different conditions.
✓ 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
Conditional Statements
Output: A is smaller
Prof. Jayakumar Sadhasivam, VIT, Vellore. 64
Switch Statement
๏ The switch statement is used to perform different actions based on
different conditions.
SYNTAX:
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
...
default:
code to be executed if n is different from all
labels;
}
Prof. Jayakumar Sadhasivam, VIT, Vellore. 67
Switch Statement
<?php
$Weather="Cloudyblurr";
switch($Weather){
case "Sunny":
echo "Weather is quite pleasent outside";
break;
case "Rainy":
echo "Its Raining outside";
break;
case "Cloudy":
echo "It is expected to Rain";
break;
default:
echo "Weather Can not be forecast";
break;
} Output:
?> Weather Can not be forecast
Prof. Jayakumar Sadhasivam, VIT, Vellore. 68
Switch Statement
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red,
blue, nor green!";
}
?> Output:
Your favorite color is red!
Prof. Jayakumar Sadhasivam, VIT, Vellore. 69
PHP Loops
PHP 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
Prof. Jayakumar Sadhasivam, VIT, Vellore. 71
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;
}
while Loop
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
} Output:
?> The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
while Loop
<?php
$x = 0;
while($x <= 100) {
echo "The number is: $x <br>";
$x+=10;
Output:
} The number is: 0
?> The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
The number is: 60
The number is: 70
The number is: 80
The number is: 90
The number is: 100
Prof. Jayakumar Sadhasivam, VIT, Vellore. 74
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);
do...while Loop
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5); Output:
?> The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
do...while Loop
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5); //Condition Fails
?>
Output:
The number is: 6
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
for Loop
<?php
for ($x = 0; $x <= 5; $x++) {
echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
for Loop
<?php
for ($x = 5; $x > 1; $x--) {
echo "The number is: $x <br>";
}
?>
Output:
The number is: 5
The number is: 4
The number is: 3
The number is: 2
for Loop
<?php
for ($x = 0; $x <= 50; $x+=10) {
echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
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;
}
foreach Loop
<?php
$colors = array("red", "green", "blue",
"yellow");
foreach Loop
<?php
$age = array("Peter"=>"35",
"Ben"=>"37", "Joe"=>"43");
Break Statement
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
//Break the statement, when it reaches X=4.
break;
}
echo "The number is: $x <br>";
}
?> Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
Continue Statement
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue; //when it reaches the value x=4,
it skip that value and continue to next iteration
}
echo "The number is: $x <br>";
} Output:
?> The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
Prof. Jayakumar Sadhasivam, VIT, Vellore. 87
Arrays
Arrays
๏ An array stores multiple values in one single variable.
๏ In PHP, there are three types of arrays:
✓ Indexed/Numeric arrays - Arrays with a numeric index
✓ Associative arrays - Arrays with named keys
✓ Multidimensional arrays - Arrays containing one or more
arrays
Output: 3
Indexed/Numeric Arrays
๏ A numeric array stores each array element with a numeric
index.
๏ There are two ways to create indexed arrays:
✓ The index can be assigned automatically (index always
starts at 0)
$cars = array("Volvo", "BMW", "Toyota");
✓ The index can be assigned manually
$cars[10] = "Volvo";
$cars[11] = "BMW";
$cars[12] = "Toyota";
Indexed/Numeric Arrays
<?php
$cars = array("Volvo", "BMW", "Toyota");
Output:
I like Volvo, BMW and Toyota.
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
Associative Arrays
๏ An associative array, each ID key is associated with a value.
๏ When storing data about specific named values, a numerical array
is not always the best way to do it.
๏ With associative arrays we can use the values as keys and assign
values to them
Associative Arrays
๏ There are two ways to create an associative array:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Associative Arrays
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Output:
Peter is 35 years old.
Associative Arrays
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
Multidimensional Arrays
๏ In a multidimensional array, each element in the main array can
also be an array.
๏ Each element in the sub-array can be an array, and so on.
Multidimensional Arrays
PHP Functions
PHP Functions
๏ PHP Built-in Functions
✓ PHP has over 1000 built-in functions that can be called directly, from
within a script, to perform a specific task.
✴ Array functions
✴ Calendar functions
✴ Date functions
✴ Directory functions
✴ Error functions
✴ Mail functions
✴ Math functions
✴ Misc functions
✴ MySQL functions
Prof. Jayakumar Sadhasivam, VIT, Vellore. 106
PHP Functions
๏ PHP User Defined Functions
• Besides the built-in PHP functions, it is possible to create
your own functions.
✓ A function is a block of statements that can be used
repeatedly in a program.
✓ A function will not execute automatically when a page
loads.
✓ A function will be executed by a call to the function.
fi
fi
fi
output:
30
fi
function familyName($fname) {
code to be executed;
}
TIME Function
๏ a (lowercase A): am or pm depending on the time.
๏ A (uppercase A): AM or PM depending on the time.
๏ g (lowercase G): Hour without leading zeroes. Values are 1 through
12.
๏ G (Uppercase G): Hour in 24-hour format without leading zeroes.
Values are 0 through 23.
๏ h (lowercase H): Hour with leading zeroes. Values 01 through 12.
๏ H (uppercase H): Hour in 24-hour format with leading zeroes.
Values 00 through 23.
๏ i (lowercase I): Minute with leading zeroes. Values 00 through 59.
๏ s (lowecase S): Seconds with leading zeroes. Values 00 through 59.
Prof. Jayakumar Sadhasivam, VIT, Vellore. 119
DATE Functions
๏ d (lowercase D): Day of the month with leading zeroes. Values
are 01 through 31.
๏ J (lowercase J): Day of the month without leading zeroes. Values
1 through 31
๏ D (uppercase D): Day of the week abbreviations. Sun through Sat
๏ l (lowercase L): Day of the week. Values Sunday through
Saturday
๏ w (lowercase W): Day of the week without leading zeroes. Values
0 through 6.
๏ z (lowercase Z): Day of the year without leading zeroes. Values 0
through 365.
Prof. Jayakumar Sadhasivam, VIT, Vellore. 120
MONTH Function
๏ m (lowercase M): Month number with leading zeroes. Values 01
through 12
๏ n (lowercase N): Month number without leading zeroes. Values 1
through 12
๏ M (uppercase M): Abbreviation for the month. Values Jan through
Dec
๏ F (uppercase F): Normal month representation. Values January
through December.
๏ t (lowercase T): The number of days in the month. Values 28
through 31.
YEAR Function
๏ L (uppercase L): 1 if it's a leap year and 0 if it isn't.
๏ Y (uppercase Y): A four digit year format. Values 2020
๏ y (lowercase Y): A two digit year format. Values 00 through 99.
๏ S (uppercase S): English ordinal suffix for the day of the month, 2
character. Values st, nd, rd or th. Works well with j
Example
<?php
echo date("m/d/y");
?>
Output: 07/15/20
<?php
echo date('l \t\h\e jS');
?>
Check Date
๏ Syntax:
checkdate(month, day, year)
<?php
var_dump(check4date(07,15,2020));
?>
Output: bool(true)
Example
<?php
echo date("m/d/y");
echo "<br>";
echo "Today is ";
echo date("j F Y, \a\\t G.ia");
?>
Output:
07/15/20
Today is 15 July 2020, at 8.45am
PHP $GLOBALS
๏ $GLOBALS is a PHP super global variable which is used to access global
variables from anywhere in the PHP script.
๏ PHP stores all global variables in an array called $GLOBALS[index]. The
index holds the name of the variable.
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z; Output: 100
?>
Prof. Jayakumar Sadhasivam, VIT, Vellore. 131
<?php
$MyNumber=456456; // Local Scope
echo "Number is : $MyNumber \n”;
function Addition(){
global $MyNumber; //Assigning $MyNumber as Global
Variable
$a=50;
$b=7;
$c=$a * $b;
echo "New Global Addition : ".($MyNumber * $c);
echo "\nAddition is {$c}";
} Output:
Number is : 456456
Addition();
New Global Addition : 159759600
?> Addition is 350
PHP $_SERVER
๏ $_SERVER is a PHP super global variable which holds information
about headers, paths, and script locations created by the Web server.
๏ Information regarding the server and client configuration and the
current request environment.
๏ $_SERVER[‘PHP_SELF’]: Returns the filename of the currently
executing script
๏ $_SERVER['HTTP_REFERER']: The URL of the page that
referred the user to the current location.
๏ $_SERVER['REQUEST_URI']: The path component of the URL.
For example, if the URL is http://www.example.com/blog/apache/
index.html, then the URI is /blog/apache/index.html.
PHP $_SERVER
๏ $_SERVER['REMOTE_ADDR']: The client’s IP address.
๏ $_SERVER['HTTP_USER_AGENT']: The client’s user agent, which
typically offers information about both the operating system and
browser.
๏ $_SERVER[‘SERVER_SOFTWARE’] : Returns the server
identification string (such as Apache/2.2.24)
๏ $_SERVER[‘SERVER_ADDR’] : Returns the IP address of the host
server
๏ $_SERVER[‘SERVER_NAME'] : Returns the name of the host server
(such as jayakumars.in)
๏ $_SERVER[‘GATEWAY_INTERFACE’]: Returns the version of the
Common Gateway Interface (CGI) the server is using
Prof. Jayakumar Sadhasivam, VIT, Vellore. 134
PHP $_REQUEST
๏ PHP $_REQUEST is a PHP super global variable which is used to
collect data after submitting an HTML form.
๏ The form with an input field and a submit button. When a user
submits the data by clicking on "Submit", the form data is sent to
the file specified in the action attribute of the <form> tag. In this
example, we point to this file itself for processing form data. If
you wish to use another PHP file to process form data, replace
that with the filename of your choice. Then, we can use the super
global variable $_REQUEST to collect the value of the input
field.
<body>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type=“submit"> </form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
} ?>
</body>
Prof. Jayakumar Sadhasivam, VIT, Vellore. 136
<body>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = htmlspecialchars($_REQUEST['fname']);
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
} ?>
</body>
Prof. Jayakumar Sadhasivam, VIT, Vellore. 137
$_GET
๏ PHP $_GET is a PHP super global variable which is used to
collect form data after submitting an HTML form with
method="get".
๏ $_GET can also collect data sent in the URL.
๏ http://www.example.com/index.html?cat=apache&id=157 was
requested, you could access the following variables by using the
$_GET superglobal:
✓$_GET['cat'] = "apache"
✓$_GET['id'] = "157
$_POST
๏ The $_POST superglobal contains information pertinent to
any parameters passed using the POST method.
๏ The following POST variables will be made available via the
target subscribe.php script:
✓ $_POST['email'] = "jayakumars@vit.ac.in";
✓ $_POST['pswd'] = "rainyday";
✓ $_POST['subscribe'] = “subscribe!”;
๏ URL: http://www.example.com/index.html
$_COOKIE
๏ The $_COOKIE superglobal stores information passed into
the script through HTTP cookies.
๏ Such cookies are typically set by a previously executed
PHP script through the PHP function setcookie().
๏ For example, suppose that you use setcookie() to store a
cookie named example.com with the value ab2213.
๏ You could later retrieve that value by calling
$_COOKIE["example.com"]
$_SESSION
๏ The $_SESSION superglobal contains information regarding all
session variables.
๏ Registering session information allows you the convenience of
referring to it throughout your entire Web site, without the hassle
of explicitly passing the data via GET or POST.
๏ Sessions are stored on the server, which means clients do not have
access to the information you store about them.
๏ They work instead like a token allowing access and passing
information while the user has their browser open.
๏ The problem with sessions is that when you close your browser
you also lose the session.
Prof. Jayakumar Sadhasivam, VIT, Vellore. 144
$_FILES
๏ The $_FILES superglobal contains information regarding data
uploaded to the server via the POST method.
$_FILES
๏ UPLOAD_ERR_OK: The file was successfully uploaded.
๏ UPLOAD_ERR_INI_SIZE: The file size exceeds the maximum
size imposed by the upload_max_filesize directive.
๏ UPLOAD_ERR_FORM_SIZE: The file size exceeds the
maximum size imposed by an optional MAX_FILE_SIZE hidden
form-field parameter.
๏ UPLOAD_ERR_PARTIAL: The file was only partially uploaded.
๏ UPLOAD_ERR_NO_FILE: A file was not specified in the upload
form prompt.
$_FILES - Example
<?php
if ($_FILES["file"]["error"] > 0)
{ echo "Error: ".$_FILES["file"]["error"]; }
else {
echo "Upload: ".$_FILES["file"]["name"];
echo "Type: " . $_FILES["file"]["type"];
echo "Size: ". ($_FILES["file"]["size"] / 1024) ." Kb";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
$_ENV
๏ The $_ENV superglobal offers information regarding the PHP
parser’s underlying server environ-ment.
๏ Some of the variables found in this array include:
Local Variables
๏ A variable declared in a function is considered local.
๏ That is, it can be referenced only in that function.
๏ Any assignment outside of that function will be considered to
be an entirely different variable from the one contained in the
function.
๏ Note that when you exit the function in which a local variable
has been declared, that variable and its corresponding value
are destroyed.
Local Variables
<?
$x = 4;
function assignx () {
$x = 0;
echo "\$x inside function is $x. <br>";
}
assignx();
echo "\$x outside of function is $x. <br>";
?>
Output:
$x inside function is 0.
$x outside of function is 4.
Static Variables
๏ In contrast to the variables declared as function parameters, which
are destroyed on the function’s exit, a static variable does not lose
its value when the function exits, and will still hold that value if
the function is called again.
๏ You can declare a variable as static simply by placing the
keyword STATIC in front of the variable name.
<? <?
function static_track() function static_track(
{ {
STATIC $count = 0; $count = 0; //not using
$count++; STATIC keyword
print $count; $count++;
print "<br>"; print $count;
} print "<br>";
static_track(); }
static_track(); static_track();
static_track(); static_track();
?> Output:
static_track(); Output:
1 ?> 1
2 1
3 1
Prof. Jayakumar Sadhasivam, VIT, Vellore. 152
File Handling
File Handling
๏ What is a file?
• A computer file is a block of information, or resource, which is
available to a computer program.
• How to create a File? [Linux Example]
• File Permissions [Read, Write, Execute]
• File Types
✓ Text File [ASCII, Unicode ]
✓ Media File [Mp3, Video, Flash]
✓ System Files [Dynamic Link Library – DLL]
✓ Executable Files [Exe, Deb, Rpm]
File Handling
Syntax:
$filename = "sample.txt";
$fp = fopen($filename,"r") or
exit("Unable to open file");
ank You !!
😎
Th
!