-
-
1 Birthday Calculate
A- $genDate2=date(‘Y-m-d H:i:s’, mktime(23,59,59,11,11,2050));
B- $dd=date(“d”, strtotime($genDate));
$mm=date(“m”, strtotime($genDate));
$yyyy=date(“Y”, strtotime($genDate));
$genDate2=date(‘Y-m-d H:i:s’, mktime(23,59,59,$mm,$dd,$yyyy));
C: $doj=’2001-12-12 05:02:03′;
$d=date(‘d’, strtotime($doj));
$m=date(‘m’, strtotime($doj));
$y=date(‘Y’, strtotime($doj));
$num_days = 10*60*60*24;
$rr = date(“Y-m-d”, mktime(0, 0, 0, $d, $m, $y)+$num_days);
d $endDate2=date(‘Y-m-d’);
$beginDate2=date(‘Y-m-d’,strtotime($record['mem_doj']));
function daysDifference($endDate,$beginDate)
{
//explode the date by “-” and storing to array
$date_parts1=explode(“-”, $beginDate);
$date_parts2=explode(“-”, $endDate);
//gregoriantojd() Converts a Gregorian date to Julian Day Count
$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
return $end_date – $start_date;
}
2- exceed execution time
ini_set(‘max_execution_time’,2000);
3- Resume upload with particular exteneion
$upload = $_POST['upload'];
$resume = $_FILES['resumeUpload']['name'];
if(isset($upload))
{
$basename=basename($_FILES['resumeUpload']['name']);
$ext=substr($basename,strpos($basename,’.')+1);
if($ext==doc || $ext==docx)
{
move_uploaded_file($_FILES['resumeUpload']['tmp_name'],”uploads/resumes/”.$basename);
}
else
{
echo ‘error’;
}
}
5:-Set online and local path
$host=$_SERVER['HTTP_HOST'];
if($host==’unicodesystems.in’ || $host==’www.unicodesystems.in’)
{
$path=’unicodesystems.in’;
}
else{
$req=’localhost’;
$path=$host.’/’.$req.’/';
}
6- File Handling and reading
$fmarquee = fopen(“../marquee.txt”,”r”) or die(“Could not open file”);
while(!feof($fmarquee))
{
$marquee = $marquee.’ ‘.fgets($fmarquee);
}
fclose($fmarquee)
7-”define” keyword in PHP
define (“ROOT_ID”,”5000121″);
echo ROOT_ID;
OR
define(“APP_BASE”,”..”);
define(“INCLUDE_BASE”,APP_BASE.”/includes”);
define(“TEMP_BASE”,APP_BASE.”/templates”);
include INCLUDE_BASE.”/Config.php”;
8- How to create Class and constructor in php
class DefineKeyword
{
public $name=54;
function __construct()
{
print “In constructor\n”;
echo $this->name=56;
}
public function hello2($root)
{
return ‘this is function -2′.$this->name;;
}
public function hello($root)
{
$r2=$this->hello2($root);
return $r2.’<br/>this is function -1′;
}
}
$r1=new DefineKeyword();
echo $r1->hello(5);
9)Calculating a php script memory usages
<?php echo memory_get_usages;?>
10)The Isset command
OR
“undefined Variable warning in php programs”
- One common cause is not using Issetthe isset command is very useful when you want run some code and somewarning such as “undefined variable” comes especially when it comes to POSTvariables , then isset comes to rescue.the syntax of isset command
if(isset($_SESSION['views']){// your code goes here
}
11)How to use content of one php file into other php file
You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.
The two functions are identical in every way, except how they handle errors:
- include() generates a warning, but the script will continue execution
- require() generates a fatal error, and the script will stop
Assuming that header.php is in same directory as new php
php include(“header.php”)Welcome to my home page;
12)How to validate email by the use of regular expression
<?php
$email=$_Post['email'];
if(preg_match(“~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).([a-zA-Z0-9]{2,4})~”,$email)){
echo “this is a valid email”;
}
else
{
echo ‘This is an invalid email.’;
}?>
13)How to raed and write csv through Php Code
fgetcsv — Gets line from file pointer and parse for CSV fields.
fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read.<br>
This works on PHP 4 and PHP 5.
14)How to do a page refresh in php
the page below can be called in any html link or page and on clicking the page refreshes
echo “Thank You for visiting.”;
echo “”;
echo “You will be redirected to the previous page after (4) seconds.”;
//content indicates number of seconds
//url indicates the page to be redirected
//this page will be redirected to refresh.html after 4 seconds
echo “<meta http-equiv=Refresh content=4;url=refresh.html>”;
?>
15)How to use OrderBy in php mysql interaction?
The below code lets you connect to a mysql database with a host name root and some password. The Database used name is my_db and then result of select on a table person with ordered according to age field
<?php
$con = mysql_connect(“localhost”,”root”,”passwoerd”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“my_db”, $con);
$result = mysql_query(“SELECT * FROM Persons ORDER BY age”);
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
echo ” ” . $row['LastName'];
echo ” ” . $row['Age'];
echo “<br />”;
}
mysql_close($con);
?>
16)How to update some records in a mysql database through php query
The below code lets you connect to a mysql database with a host name root and some password. The Database used name is my_db and then result of update the table person fields with firstname and lastname with data in the query.It is important to close any existing database connection with mysql_close
<?php
$con = mysql_connect(“localhost”,”root”,”password_if_any”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“my_db”, $con);
mysql_query(“UPDATE Persons SET Age = ’36′
WHERE FirstName = ‘john’ AND LastName = ‘Ramsay’”);
mysql_close($con);
?>
17)How to delete some records in a mysql database through php query
The below code lets you connect to a mysql database with a host name root and some password. The Database used name is my_db and then result of deleting the table person those fields with lastname as “Ramsay” in the query.It is important to close any existing database connection with mysql_close
<?php
$con = mysql_connect(“localhost”,”root”,”password_if_any”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“my_db”, $con);
mysql_query(“DELETE FROM Persons WHERE LastName=’Ramsay’”);
mysql_close($con);
?>
18)How to send email through Php script
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if form is filled out with email as field name
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail(“abc@hgy.com”, “Subject: $subject”,
$message, “From: $email” );
echo “Thank you for using our mail form”;
}
else
//if form is filled out with email as field name display the form and fill email //into it
{
echo “<form method=’post’ action=’mailform.php’>
Email: <input name=’email’ type=’text’ /><br />
Subject: <input name=’subject’ type=’text’ /><br />
Message:<br />
<textarea name=’message’ rows=’15′ cols=’40′>
</textarea><br />
<input type=’submit’ />
</form>”;
}
?>
</body>
</html>
19)How to combine two arrays in php through array_combine method ?
Or
How to combine two arrays in php with one array for keys and another for value?
Here we will talk about a particular function called array_combine.The array_combine() function creates an array by combining two other arrays, where the first array is the keys, and the other array is the values.
The syntax of array_combine method is :-
array_combine(array1,array2);
array1 -> Required. An array, specifying the keys
array2 -> Required. An array, specifying the values
An example of a array_combine is
On combining the two arrays we will get
Array ( [a] => jon [b] => charles [c] => Harry[d] => san)
20)How to merge two arrays with array_merge method ?
Or
How to combine two or more arrays in php ?
The array_merge() function merges one ore more arrays into one array
The syntax of this function is :-
array_merge(array1,array2,array3…)
Parameter Description
array1 -> Required. Specifies an array
array2 -> Optional. Specifies an array
array3 -> Optional. Specifies an array
If two or more array elements have the same key, the last one overrides the others .
An example of the array_merge function will be ->
“Horse”,”b”=>”Dog”);
$a2=array(“c”=>”Cow”,”b”=>”Cat”);
print_r(array_merge($a1,$a2));
?>
The Output of above code is
Array ( [a] => Horse [b] => Cat [c] => Cow )
21)How to display an image with specified image, height and width in percentage with help of gd library
Following we see a function ResizeImage with height and width of image that we can give to which percent it will be resized
<?php
function ResizeImage($image ,$width ,$height)
{
//image resizer by myscripting
//get the size of the original
$size = GetImageSize($image);
//divide the width / height percentage by 100
$new_width = 100 / $width;
$new_height = 100 / $height;
//store the resized dimensions in a variable
$sizeh = $size[1]/ $new_height;
$sizew= $size[0]/ $new_width;
//display the new resized image
$new_image = “<img src = \”$image\” height=\”$sizeh\” width =\”$sizew\”>”;
echo $new_image;
}
?>
Now we can use the above function to display the modified image
<?php
require(“imageresize.php”);
//this reduces the image
ResizeImage(“1.gif”,50,50);
echo “<br>”;
//we can increase an image as well
ResizeImage(“1.gif”,150,150);
?>
