Professional Documents
Culture Documents
Quest : Print
1
12
123
1234
12345
<?php
for ($i=1; $i<=5; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j." ";
}
echo "<br/>";
}
?>
Output:
12
123
1234
12345
Print
*
**
***
****
*****
<?php
for ($i=1; $i<=5; $i++)
{
for ($k=5; $k>$i; $k--)
{
Output:
*
**
***
****
*****
<?php
$sum=0;
for($i=1;$i<=100;$i++)
{
if($i%2==0)
{
$sum=$sum+$i;
}
}
echo $sum;
?>
Output: 2550
<?php
$sum=0;
for ($i=1; $i<=100; $i++)
{
$sum=$sum+$i;
}
echo $sum;
?>
Output: 5050
Output: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86
88 90 92 94 96 98 100
<?php
for ($i=1; $i<=99; $i+=2)
{
echo $i." ";
}
?>
Output: 1 3 5 7 9 ... 99
Output:
Output:
<?php
@$f=$_GET['f'];
@$s=$_GET['s'];
for ($i=1; $i<=$s; $i++)
{
$f--;
}
echo "Subtraction of given numbers=".$f;
?>
<html>
<body>
<form>
Enter first number
<input type="text" name="f"><br/>
Enter Second number
<input type="text" name="s"><br/>
<input type="submit" value="Subtract">
</form>
<body>
Output:
while ($i<=10);
?>
<body>
<form>
Enter Your table
<input type="text" name="tab"><br/>
<input type="submit" value="Table">
</form> </body>
Online Translator
This is Online translator .Its easy to translate your site to any language.You can fix
to any position in this translator .it consists above 40 language.You can create easy
Translator .
just copy and paste a j-query code and fix to any place your site.
J-query:
<script type="text/javascript" src="http://x.translateth.is/translate-this.js"></script>
<script type="text/javascript>TranslateThis();</script>
Html:
<div id="translate-this" style="width:250px; margin-top:3px;border:0 red solid;
float:right; text-align:right;">
<a style="width:180px;height:18px;display:block;"class="translate-thisbutton"href="http://www.abc.com/"></a>
</div>
Function
What it Does
print_r()
<?php
str=array("welcome","student");
print_r($str);
?>
Output : Array([0]=>"welcome",[1]=>"student")
explode()
<?php
$str="welcome user how r you";
//print_r($str);
$arr=explode(" ",$str);
print_r($arr);
//explode
$val="10,20,30,40,50,60";
$arr1=explode(",",$val);
foreach($arr1 as $v)
{
@$s+=$v;
}
echo $s;
?>
Output : Array([0]=>"welcome",[1]=>"student")
implode()
<?php
$arr=array("hello","user");
$str=implode(" ",$arr);
echo $str;
?>
count()
<?php
$arr=array(10,20,30,40,50);
echo "Number of elements= ".count($arr)."<br/>";
range()
<?php
$range=range("A","M");
print_r($range);
?>
max()
<?php
$max=array(10,20,30,40,100,50);
echo max($max);
?>
Output : 100
min()
<?php
$max=array(10,20,30,40,100,50);
echo min($max);
?>
Output : 10
shuffle()
<?php
$arr=range("a","z");
shuffle($arr);
print_r($arr);
?>
array_rand()
<?php
$arr=range("a","z");
$rand=array_rand($arr);
echo $rand;
?>
array_slice()
<?php
$color=array("blue","red","green","pink");
$slice=array_slice($color,1,2);
print_r($slice);
?>
array_shift()
<?php
$city=array("delhi","patna","noida");
array_shift($city);
print_r($city);
?>
array_unshift()
<?php
$city=array("delhi","patna","noida");
array_unshift($city,"banglore");
print_r($city);
?>
array_pop()
<?php
$city=array("delhi","patna","noida");
array_pop($city);
print_r($city);
?>
array_push()
<?php
$city=array("delhi","patna","noida");
array_push($city,"gurgaon");
print_r($city);
?>
array_unique()
<?php
$city=array("delhi","patna","noida","delhi","patna");
$uni=array_unique($city);
print_r($uni);
?>
Output : Array ( [0] => delhi [1] => patna [2] =>
noida )
array_reverse()
<?php
$city=array("delhi","patna","noida");
$rev=array_reverse($city);
print_r($rev);
?>
Output : Array ( [0] => noida [1] => patna [2] =>
delhi )
array_merge()
<?php
$city=array("delhi","patna");
$city1=array("gurgaon","banglore");
$merg=array_merge($city,$city);
print_r($merg);
?>
Output : Array ( [0] => delhi [1] => patna [2] =>
gurgaon [3] => banglore)
array_intersect()
<?php
$arr1=array("red","blue","pink");
$arr2=array("black","blue","white");
$common=array_intersect($arr1,$arr2);
print_r($common);
?>
array_diff()
<?php
$arr1=array("red","blue","pink");
$arr2=array("black","blue","white");
$diff=array_diff($arr2,$arr1);
print_r($diff);
?>
in_array()
<?php
$arr1=array("red","blue","pink");
echo in_array("red",$arr1);
?>
Output : 1
array_key_exists()
array
<?php
$arr1=array("red","blue","pink");
echo array_key_exists(1,$arr1);
?>
Output : 1
sort()
<?php
$arr=array(100,11,5,12,150,0);
sort($arr);
print_r($arr);
?>
Sorts an array
rsort()
<?php
$arr=array(100,11,5,12,150,0);
rsort($arr);
print_r($arr);
?>
Output : Array ( [0] => 150 [1] => 100 [2] => 12
[3] => 11 [4] => 5 [5] => 0 )
asort()
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green");
asort($col);
print_r($col);
?>
Output : Array ( [z] => blue [y] => green [a] => red
)
arsort()
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green");
arsort($col);
print_r($col);
?>
Output : Array ( [a] => red [y] => green [z] => blue
)
ksort()
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green");
ksort($col);
print_r($col);
?>
Output : Array ( [a] => red [y] => green [z] => blue
)
krsort()
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green");
krsort($col);
print_r($col);
?>
Output : Array ( [z] => blue [y] => green [a] => red
)
array_product()
<?php
$a=array(5,5,5,4);
echo(array_product($a));
?>
Output : 500
array_sum()
<?php
$a=array(0=>5,1=>15,2=>25,15);
echo array_sum($a);
?>
Output : 60
array_search()
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Horse",$a);
?>
Output : c
array_splice()
<?php
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
print_r(array_splice($a1,0,2,$a2));
?>
current()
<?php
$arr=array("dog","cat","horse");
echo current($arr);
?>
Output : dog
key()
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
';
}
next($array);
}
?>
Output : fruit1
fruit4
fruit5
Php Array
Array :- A data structure that stores one or more similar type of values in a single value.
Use array() function to create array
Example:-
Numeric array - An array with a numeric index. These arrays can store numbers,
strings and any object but their index will be prepresented by numbers. By default array
index starts from zero(0).
<?php
/* 1st method to create array. */
$num = array( 1, 2, 3, 4, 5,6,7,8,9);
foreach( $num as $value )
{
echo "Value is $value <br />";
}
/* 2nd method to create array. */
$num[0] = "one";
$num[1] = "two";
$num[2] = "three";
$num[3] = "four";
$num[4] = "five";
$num[5] = "six";
$num[6] = "seven";
$num[7] = "eight";
$num[8] = "nine";
Associative array - An array with strings as index. This stores element values in
association with key values rather than in a strict linear index order.Associative array is very
similar to numeric arrays in term of functionality but they are different in terms of their
index.
Note:- Don't keep this array inside the double quote while printing it otherwise it will not
give any return value.
<?php
/* 1st method to associate create array. */
$salaries = array("ritu" => 25000,"raj" => 20000,"kumar" => 15000);
echo "Salary of ritu is ". $salaries['ritu'] . "<br />";
echo "Salary of raj is ". $salaries['raj']. "<br />";
echo "Salary of kumar is ". $salaries['kumar']. "<br />";
/* 2nd method to create array. */
$salaries['ritu'] = "high";
$salaries['raj'] = "medium";
$salaries['kumar'] = "low";
echo "Salary of ritu is ". $salaries['ritu'] . "<br />";
echo "Salary of raj is ". $salaries['raj']. "<br />";
echo "Salary of kumar is ". $salaries['kumar']. "<br />";
?>
Multidimensional array - An array containing one or more arrays and values are
accessed using multiple indices.Each element in the main array can also be an array,And
each element in the sub-array can be an array.
<?php
$marks = array("ritu" => array("physics" => 35,"maths" => 30,"chemistry" => 39),
"raj" => array
(
"physics" => 30,
4
3
2
1
Factorial of 4 is 24
To find the factorial of a number using form in php
index.php
<?php
/* Function to get Factorial of a Number */
function getFactorial($num)
{
$fact = 1;
for($i = 1; $i <= $num ;$i++)
$fact = $fact * $i;
return $fact;
}
?>
<!doctype html>
<html>
<head>
<title>Factorial Program using PHP</title>
</head>
<body>
<form action = "" method="post">
Enter the number whose factorial requires <Br />
<input type="number" name="number" id="number" maxlength="4" autofocus required/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit']) and $_POST['submit'] == "Submit")
{
if(isset($_POST['number']) and is_numeric($_POST['number']))
{
echo 'Factorial Of Number: <strong>'.getFactorial($_POST['number']).'</strong>';
}
}
?>
</body>
</html>
Output will be :
if you enter 5 and click to submit then :
max() function is a properties of an array and is used to find the largest value from an array.
Example :
max.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
rel="stylesheet" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#datepicker").datepicker();
});</script>
</head>
<body>
Date: <input type="text" id="datepicker">
</body>
</html>
$arr=array(10,11,12,13,14,15);
for($i=0;$i<count($arr);$i++)
{
if($arr[$i]%2==0)
{
@$even=$even+$arr[$i];
}
else
{
@$odd=$odd+$arr[$i];
}
}
echo "Sum of even=".$even."<br/>";
echo "Sum of odd=".$odd;
?>
}
?>
Form.php
<!DOCTYPE html>
<html dir="ltr" lang="en-US" >
<head>
<meta charset="UTF-8" />
<title>Search</title>
</head>
<body>
<h>search by name</h>
<form action="search.php" method="get">
<label>Name:
<input type="text" name="name" />
</label>
<input type="submit" value="Search" />
</form>
</body>
</html>
Search.php
<?php
$searchTerm = trim($_GET['name']);
if($searchTerm == "")
{
echo "Enter name ";
exit();
}
//database connection info
$host = "localhost";
$db = "dbname";
$user = "root";
$pwd = "ubuntu@rituraj";
$link = mysqli_connect($host, $user, $pwd, $db);
id
name
course
age
Rituraj kumar
B.Tech
23
Robi tomar
Android
24
Shikha chaudhary
Asp.Net
23
Harshu
SEO
21
Neeraj
UI Developer
23