You are on page 1of 7

Array:

Group of similar data types


Creation
1. Manual
$book[0]="Let us C";
$book[1]="PHP";
$book[2]="Balagursami";
2. Using Array Function
$book= array('Let us C','PHP','Balagursami');
3. Auto indexing/ auto slot fill
$book[]="Let us C";
$book[]="PHP";
$book[]="Balagursami";
Associative Array
$teacherofPHP['name']="Utkarsh";
$studentbefkoof['name']="PAM";
Functions
Code snippet that aceepts, produce results and return values.
Can be called
By value
By reference
Globalising the function
Done by writing the function inside a file and including that file to ph
p code wherever than function is needed
$days=include('day.php');
Lambda functions
1. No name
2. Created by create_function();
Example:
$multiply= create_function('$a,$b','return $a*$b');
It minimises chances of accidentally creating 2 functions of same name.
Built in functions:
1. header() //Redirect to another location or address. OR refresh a page
after certain interval of time.
printf()
include()
exit()
2. PHP information
phpinfo()
Parameters
1. INFO_GENERAL
2. INFO_CREDIT
3. INFO_CONFIGURATION
4. INFO_ENVIRONMENT
5. INFO_VARIABLES
6. INFO_LICENCE
7. INFO_ALL
3. $_SERVER[]
Parameters
1. HTTP_HOST // Tells host name
2. HTTP_USER_AGENT //Info on browser and user agent
3. REMOTE_ADDR //Tells remote IP Address
Write a code to check if user is running Opera browser or not...
if(strpos($_SERVER["HTTP_USER_AGENT"], 'Opera')!==false)
//strpos function is used to compare the two strings.
echo "The browser is Opera";
else
echo "The browser is not Opera";
4. Variable related functions
1. isset()
2. empty()
3. unset()
5. Script Execution
1. exit()
2. eval()
3. die()
6. Array related functions
1. count()
2. in_array()
3. reset()
prev()
next()
array_walk()
4. sort()
5. explode()
implode()
6. array_push array_pop()
7. array_shift()
8. array_reverse()
7. Date and time
date("l d S F Y h:i:s A");
l=Day
d=Date (using 0 in single digit dates for eg 05, 06)// For using
date as 6,4 use D.
S=th (5th)
F=Month
Y= Year
h=hour (12 hour format)// For 24 hour format use H.
i=minutes
s=second
A= AM/PM
time()
Displays number of time seconds from reference point of time
374029374320488859
strtotime("20 May 2014);
388294834478474748
8. Math function
1. ceil
2. round
3. floor
4. abs
5. sqrt
6. pow
7. hypot
9. Randomisation
1. rand()
2. mt_rand()
mt= mersenne twister
10. constants
1. M_PI
2. M_PI_2
3. M_PI_4
4. M_1_PI
5. M_2_PI
6. M_SQRT_PI
11. String Functions
1. substr()
2. strpos()
3. stripos()
4. strstr()
5. stristr()
6. str_replace()
7. str_ireplace()
8. chr(int ASCII)
9. ord(String text)
10. count_char()
11. str_word_count()
12. Trimming Whitespace
1. trim()
2. ltrim()
3. rtrim()
SESSIONS
Used to store or change setting for a user session. It holds information about o
ne single user and is available to all pages in 1 application.
Its like a file, we open, modify and close it. During this process local compute
r knows our identity. But on internet, we are anonymous user and web server
does not know about our identity. Therefore sessions are used as temporary stora
ge for user identification and is deleted as soon as user leaves the website.
This info is temporary. For permanent storage, database is used.
Sessions works from UID for each visitor and is stored in cookie or is passed in
URL.
Starting the session
<?php session_start(); ?>
Storing variables in session
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=0;
?>
Destroying session
<?php
if(isset($_SESSION['views']))
unset($_SESSION['views']);
session_destroy();
?>
E_MAIL
mail(to, subject, message, header, parameter);
Non secure email
<h2>FEEDBACK FORM</h2>
<?php
if(!isset($_POST["submit"]))
{ ?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" >
FROM: <input type="text" name="from"><br>
Subj: <input type="text" name="subject"><br>
mess: <textarea name="message"></textarea><br>
<input type="submit" value="submit"> </form>
<?php
}
else
{ if(isset($_POST["from"]))
{
$from=$_POST["from"];
$subject=$_POST["subject"];
$message=$_POST["message"];
mail("eg.com",$subject,$message,"From: $from");
echo "Thankyou for feedback"
}
}
?>
SECURE EMAIL
<?php
function spamcheck($field)
{
$field=filter_var($field,FILTER_SANITIZE_CHECK); //Sanitize email
if(filter_var($field,FILTER_VALIDATE_EMAIL) #Check validity of email
return TRUE;
else
return FALSE;
}
?>
<h2>Feedback Form </h2>
<?php
if(!isset($_POST["submit"]))
{ ?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" >
FROM: <input type="text" name="from"><br>
Subj: <input type="text" name="subject"><br>
mess: <textarea name="message"></textarea><br>
<input type="submit" value="submit"> </form>
<?php
}
else
{
if(isset($_POST["from"]))
{
$mailcheck=spamcheck($_POST["from"]);
if($mailcheck==TRUE){
$from=$_POST["from"];
$subject=$_POST["subject"];
$message=$_POST["message"];
mail("eg.com",$subject,$message,"From: $from");
echo "Thankyou for feedback"
}
else
echo "Invalid Input";
}
}
MYSQL AND PHP
1. Open connection
<?php
$con=mysqli_connect(host,username,password,databasename);
if(mysqli_connect_errno())
echo "Fail".mysqli_connect_errno();
?>
2. Close connection
mysqli_close($con);
3. Instruction
$sql="create table persons(firstname char[30], lastname varchar[30], age
int)";
if(mysqli_query($con,$sql))
echo "Success"
else
echo "Failure";
4. Inserting value to a database
index.html
//html commands for creating a form
//In form we have 3 inputs: firstname, lastname and age. And we press su
bmit button to save it into the database.
<html><body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="fn">
Last Name: ""
Age: ""
<input type="submit" value="Submit">
</form>
</body></html>
//insert.php
<?php
$con=mysqli_connect(host,username,password,databasename);
if(mysqli_connect_errno())
echo "Fail".mysqli_connect_errno();
$fna=mysqli_real_escape_string($con,$_POST['fn'];
$lna=mysqli_real_escape_string($con,$_POST['ln'];
$age=mysqli_real_escape_string($con,$_POST['age'];
$sql="insert into person values('$fna','$lna','$age')";
if(mysqli_query($con,$sql))
echo "Success";
else
echo "Failure";
mysqli_close($con);
?>
5. Select statement-- Listing Database
//Create connection
$result=mysqli_query($con,"select * from persons");
while($row=mysqli_fetch_array($result))
{
echo $row['firstname'].' '.$row['lastname'];
echo "<br>";
}
}
//close connection
6. Display the data from database into a table
//Create connection
$result=mysqli_query($con,"select * from persons");
echo " <table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['firstname']."</td><td>".$row['lastname']."</td
>";
echo "</tr>";
}
echo "</table>";
//close connection

You might also like