Search This Blog

imagecreate() or image create in php

<?php
header("Content-Type: image/png");
$img = @imagecreate(110, 20)
  or die("you Can't Initialize new GD image stream");
$background_color = imagecolorallocate($img, 0, 0, 0);
$text_colour = imagecolorallocate($img, 233, 14, 91);
imagestring($img, 1, 5, 5, "hai how are you", $text_colour);
imagepng($img);
imagedestroy($img);
?>



scandir() in php

scan.php :

<?php
@print_r(scandir("send"));
?>


output :

Array ( [0] => . [1] => .. [2] => send_vard.php [3] => send_vcard_sms.html [4] => sendsms.html [5] => sendsms.php [6] => smser.php )

reset a directory with rewinddir() in php

 rewinddir.php :

<?php

//Open send directory
if(@$dir = opendir("send"))
{

//List files in send directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}

//resets the directory stream
rewinddir($dir);

//to check for changes

closedir($dir);
}
else
{
echo 'directory not opened';
}
?>

outputs :
 1)
directory not opened

2)


filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

readdir() in php

<?php
//Open send directory

if(@$dir = opendir("send"))

{//List files in send directory

while (($file = readdir($dir)) !== false)

{

echo "filename: ". $file ."<br />";

}

closedir($dir);

}

else

{

echo 'directory not opened';

}

?>

outputs :
 1)
directory not opened

2)
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

opens a directory with opendir() in php

open.php :

<?php
//Open send directory
if(@$dir = opendir("send"))
{
//List files in send directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}
closedir($dir);
}
else
{
echo 'directory not opened';
}
?>

output :
 1)
directory not opened

2)
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

current directory with getcwd() in php

getcwd.php :

<?php
echo getcwd();
?>

output :

E:\xampp\htdocs\1

open files in a directory with dir() in php

<?php
//Open send directory
if(@$dir = @dir("send"))
{
//List files in send directory
while (($file = $dir->read()) !== false)
{
echo "filename: " . $file . "<br />";
}
$dir->close();
}
else
{
echo 'directory not opened';
}
?>

output :
 1)
directory not opened

2)
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

note :
"send" is a directory name

getcwd() and chdir() in php

<?php

//to Get current directory

echo '<br>current directory<br> '.getcwd()."<br />";

//Change to send directory

if(@chdir("send"))
echo '<br>directory changed to<br> '.getcwd();
else
echo '<br>directory not changed<br>';

?>

output :

current directory
C:\xampp\htdocs\1

directory changed to
C:\xampp\htdocs\1\send

when opendir() using it close with closedir() in php

closedir.php :

<?php

//Open send directory
if(@$dir = opendir("send"))
{
echo '<br>dir opened<br>';

//List files in send directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}
closedir($dir);
echo '<br>dir closed<br>';
}
else
echo 'directory not opened';
?>
=========================================
outputs :
 1)
directory not opened

2)
dir opened
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

dir closed



checking if an element is in array or not using in_array() in php

element_in_array.php :

<?php
// Checking if an Element Is in Array or not

$num=array(1,2,3,4,5,6,7,8,9,10);

$element=$_POST['element'];

if (in_array($element, $num)) {
echo 'element found';
}
else
{
echo 'element not found';
}


?>

============================================================
element_in_array_index.php :

<center><b>here checks data is in array or not</b><br><br>
<center><form action="element_in_array.php" method="POST">
<input type="textbox" name="element">
<input type="submit" name="submit">
</form>

============================================================
input :

1) give the array values in text box

2)  click in button

ex :

array :$num=array(1,2,3,4,5,6,7,8,9,10);

here 0-9 array indexes
and
1-10 array values
 
============================================================
output :

1) element found

2) element not found

check array index or key with user inputs in php

check_array_key.php :

<center>
<?php

$num1=array(1,2,3,4,5,6,7,8,9,10,'a'=>'A');
$key=@$_POST['key'];

if (array_key_exists($key, $num1))
{
echo 'array index/key :<b> '.$key.' </b> found<br>';
}
else
{
echo 'array index/key :<b> '.$key.' </b> not found<br>';
}

?>
==========================================================

check_array_index.php :

<center><b>here checks index is in array or not</b><br><br>
<form action="check_array_key.php" method="POST">
<input type="textbox" name="key">
<input type="submit" name="submit">
</form>

============================================================
input :

1) give the array index value in text box
2) click on button

ex :

$num1=array(1,2,3,4,5,6,7,8,9,10,'a'=>'A');

here "0-9" and "a" are  indexes

"1-10" and "A" are values

============================================================

output :

array index/key : not found

array index/key : 1 found

delete array elements with unset() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>array before delete elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
echo '<br><br>';

//delete array element

unset($num[3]);
unset($num[0],$num[1],$num[2]);
echo '<b>array after delete elements (1,2,3,4) :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}


?>

output :

array before delete elements : 1 2 3 4 5 6 7 8 9 10

array after delete elements (1,2,3,4) : 5 6 7 8 9 10

change array size using array_pad() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>array before grow elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
echo '<br><br>';

$num = array_pad($num, 20,'a') ; //only additionally adding elements have 'a' value

// $num = array_pad($num, 20,'') ; additionally adding elements have empty

echo '<b>array after grow elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}


?>


output :

array before grow elements : 1 2 3 4 5 6 7 8 9 10

array after grow elements : 1 2 3 4 5 6 7 8 9 10 a a a a a a a a a a

 note :

1) just modify  " $num = array_pad($num, 20,'a') ;  " as "  $num = array_pad($num, 20,'') ; "  for empty
2) that means array index raise but with empty values

array to string in php

<?php

$num1=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>the array :</b>';
foreach($num1 as $num)
{
echo ' '.$num;
}


$string = join(',', $num1);
echo '<br><b>the string :</b>'.$string;

?>

output :

the array : 1 2 3 4 5 6 7 8 9 10

the string :1,2,3,4,5,6,7,8,9,10

array reduce with array_splice() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>original array :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 1 element :</b>';
$num = array_splice($num, 1) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 2 elements :</b>';
$num = array_splice($num, 2) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 3 elements :</b>';
$num = array_splice($num, 3) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 4 elements :</b>';
$num = array_splice($num, 4) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 5 elements :</b>';
$num = array_splice($num, 5) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 6 elements :</b>';
$num = array_splice($num, 6) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

?>

output :

original array : 1 2 3 4 5 6 7 8 9 10

array after reduce 1 element : 2 3 4 5 6 7 8 9 10

array after reduce 2 elements : 3 4 5 6 7 8 9 10

array after reduce 3 elements : 4 5 6 7 8 9 10

array after reduce 4 elements : 5 6 7 8 9 10

array after reduce 5 elements : 6 7 8 9 10

array after reduce 6 elements : 7 8 9 10

add one arry to another array or append array in php

<?php
echo '<b>the first array :</b>';
$num1=array(1,2,3,4,5,6,7,8,9,10);
foreach($num1 as $num)
{
echo ' '.$num;
}
echo '<br><b>the second array :</b>';
$num2=array(11,12,13,14,15,16,17,18,19,20);
foreach($num2 as $num)
{
echo ' '.$num;
}
echo '<br><br><b>append the first array to the second array : </b>';

$num3 = array_merge($num1, $num2);

foreach($num3 as $num)
{
echo ' '.$num;
}

?>

output :

the first array : 1 2 3 4 5 6 7 8 9 10
the second array : 11 12 13 14 15 16 17 18 19 20

append the first array to the second array : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Share Links

Related Posts Plugin for WordPress, Blogger...