PHP Notes
PHP Notes
Pre-requisites:
1.XAMPP software :
2.Web browser :
3.Editors : or
What is PHP:
➢ PHP is a server-side scripting language designed for web development.
Why PHP?
➢ PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
➢ PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
➢ PHP supports a wide range of databases
Example Program:
<?php
$a=10;
echo $a;
?>
o/p:10
<?php
$a=10.9;
<?php
$a=”welcome”;
echo $a;
PHP Arrays
➢ An array is a special variable, which can hold more than one value at a time.
➢ In PHP, the array() function is used to create an array:
Indexed arrays:-
Arrays with a numeric index: The following example creates an indexed
array named $cars, assigns three elements to it, and then prints a text
containing the array values:
<?php
$fruits = array("Mango", "Apple", "Sapota");
echo "I like " . $fruits[0] . ", ". $fruits[1];
?>
o/p: I like Mango,Apple
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
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
➢ PHP allows you to associate name/label with each array elements in PHP
using => symbol.
➢ Such way, you can easily remember the element because each element is
represented by label than an incremented number.
<?php
$cseroll=array("kiran"=>"30","Vamshi"=>"45","Rishi"=>"60");
echo "Kiran rollno is: ".$cseroll["kiran”]."<br/>";
echo "Vamshi rollno is: ".$cseroll["Vamshi"]."<br/>";
echo "Rishi rollno is: ".$cseroll["Rishi"]."<br/>";
?>
o/p: Kiran rollno is: 30
Vamshi rollno is: 45
Rishi rollno is: 60
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
PHP supports multidimensional arrays that are two, three, four, five, or more levels
deep.
<?php
$cars = array (array ("Volvo", 22, 18),
array ("BMW", 15, 13));
echo $cars[0][0].”<br>”;
echo $cars[1][0];
?>
o/p: Volvo
BMW
Control Structures:
PHP If Statement
PHP if statement allows conditional execution of code. It is executed if condition is
true.
If statement is used to executes the block of code exist inside the if statement only
if the specified condition is true.
Ex:
<?php
$num=12;
if($num<100){
echo "$num is less than 100";
}
?>
<?php
$num=12;
if($num%2==0){
echo "$num is even number";
}else{
echo "$num is odd number";
}
?>
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!";
}
?>
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.
While: The while loop - Loops through a block of code as long as the
specified condition is true.
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Example Explained
• $x = 1; - Initialize the loop counter ($x), and set the start value to 1
• $x <= 5 - Continue the loop as long as $x is less than or equal to 5
• $x++; - Increase the loop counter value by 1 for each iteration
Do-while Example:
The do...while loop - Loops through a block of code once, and
then repeats the loop as long as the specified condition is true.
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
<?php
function Hi($name){
echo "Hello $name<br/>";
}
Hi("Benn");
?>
1. <?php
2. <?php
echo strlen("Hello");
?> o/p: 5
3. <?php
echo strtolower("Hello WORLD.");
?> hello world
4 .<?php
echo strtoupper("Hello world");
?> o/p: HELLO WORLD
5. <?php
echo ucfirst("hello world!");
?> o/p: Hello world!
6.<?php
echo lcfirst(“Welcome”);
$_GET or $_POST.
The form request may be get or post. To retrieve data from get request, we need to use
$_GET, for post request $_POST.
The data passed through post request is not visible on the URL browser so it is secured. You
can send large amount of data through post request.
Welcomes.php
MySQL Connection Using PHP Script
❖ PHP provides mysqli_connect() function to open a database connection.
❖ This function takes five parameters and returns a MySQL link identifier on
success or FALSE on failure.
Without the requirements above, the file upload will not work.
The type="file" attribute of the <input> tag shows the input field as a file-select
control, with a "Browse" button next to the input control.
-----------------------------------------------------------
move_uploaded_file() function
The move_uploaded_file() function moves the uploaded file to a new location. The
move_uploaded_file() function checks internally if the file is uploaded thorough
the POST request. It moves the file if it is uploaded through the POST request.
send.php
Receive.php:
File Handling in PHP
• Opening File
• Reading File
• Writing File
• Closing File
Modes
File operations:
1.Create a file
<?php
$new=”cse.txt”;
Fopen($new,’w’);
?>
$new=”cse.txt”;
$result=fopen($new,’w’);
$data=”welcome”;
Fwrite($result,$data);
?>
$myfile=fopen(“cse.txt”,’r’);
Echo fgets($myfile);
?>
$myfile=”cse.txt”;
$result=fopen($myfile,’a’);
$data=”To CSE”;
Fwrite($result,$data);
?>
$myfile=”cse.txt”;
Unlink($myfile);
?>
PHP Sessions
A session is a way to store information (in variables) to be used across
multiple pages.
When you work with an application, you open it, do some changes, and then
you close it. This is much like a Session. The computer knows who you are. It
knows when you start the application and when you end. But on the internet
there is one problem: the web server does not know who you are or what you
do, because the HTTP address doesn't maintain state.
So; Session variables hold information about one single user, and are available
to all pages in one application.
Session variables are set with the PHP global variable: $_SESSION.
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
?>
</body>
</html>
PHP Cookies
What is a Cookie?
A cookie is often used to identify a user. A cookie is a
small file that the server embeds on the user's computer.
Each time the same computer requests a page with a
browser, it will send the cookie too. With PHP, you can
both create and retrieve cookie values.
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Only the name parameter is required. All other parameters are optional.