Category Archives: PHP

PHP var_dump

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

<?php

$a = array(1, 2, array(“a”, “b”, “c”));
var_dump($a);
?>

Result : –

array(3) {

[0]=>

int(1)

[1]=>

int(2)

[2]=>

array(3) {

[0]=>

string(1) “a”

[1]=>

string(1) “b”

[2]=>

string(1) “c”

}

}

——————————————————————————-

<?php

$b = 3.1;

$c = true;

var_dump($b, $c);

?>

Result: –

float(3.1)

bool(true)

Advertisements

PHP trim

<?php
$text = ” Hello “;
$textlen = strlen($text);
echo “Text is : $text \n”;
echo “Text length before trim : $textlen \n”;
$text = trim($text);
$textlen = strlen($text);
echo “Text length after trim  : $textlen \n”;
?>

Result: –

Text is :  Hello

Text length before trim : 7

Text length after trim  : 5

PHP array_count_values($arr)

array_count_values($arr)
creates a histogram for the values in an array
<?php
$states = array(“MP”,”AP”,”RJ”,”UP”,”MH”,”HM”,”AP”,”RJ”,”MP”,”MH”,”AP”);
$xstates = array_count_values($states);
ksort($xstates);
print_r($xstates);
?>
Result: –
Array
(
[AP] => 3
[HM] => 1
[MH] => 2
[MP] => 2
[RJ] => 2
[UP] => 1
)

What is PHP?

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Convert Numeric value into String in PHP

<?php
$obj = new NumberToString();
$z = array();
for ($i = 0; $i<=1000; $i++)
{
$z[] = $obj->ConvertNumber($i);
}

print_r($z);

class NumberToString
{
private static $arrNumber;

private static function GenerateArray()
{
$arr[‘0’] = “Zero”;
$arr[‘1’] = “One”;
$arr[‘2’] = “Two”;
$arr[‘3’] = “Three”;
$arr[‘4’] = “Four”;
$arr[‘5’] = “Five”;
$arr[‘6’] = “Six”;
$arr[‘7’] = “Seven”;
$arr[‘8’] = “Eight”;
$arr[‘9′] = “Nine”;
$arr[’10’] = “Ten”;
$arr[’11’] = “Eleven”;
$arr[’12’] = “Twelve”;
$arr[’13’] = “Thirteen”;
$arr[’14’] = “Fourteen”;
$arr[’15’] = “Fifteen”;
$arr[’16’] = “Sixteen”;
$arr[’17’] = “Seventeen”;
$arr[’18’] = “Eighteen”;
$arr[’19’] = “Nineteen”;
$arr[’20’] = “Twenty”;
$arr[’30’] = “Thirty”;
$arr[’40’] = “Fourty”;
$arr[’50’] = “Fifty”;
$arr[’60’] = “Sixty”;
$arr[’70’] = “Seventy”;
$arr[’80’] = “Eighty”;
$arr[’90’] = “Ninety”;
$arr[‘100’] = “Hundread”;
$arr[‘1000′] = “Thousands”;
self::$arrNumber = $arr;
}

public function ConvertNumber($Num)
{
$tmp = “”;
self::GenerateArray();
if ($Num >=0 && $Num <=20)
{
$tmp = self::$arrNumber[$Num];
}
else if (($Num == 10) || ($Num == 20) || ($Num == 30)
|| ($Num == 40) || ($Num == 50) || ($Num == 60)
|| ($Num == 70) || ($Num == 80) || ($Num == 90) || ($Num == 100) || ($Num == 1000))
{
$tmp = self::$arrNumber[$Num];
}
else if ($Num >=21 && $Num <=99)
{
$a = substr($Num,0,1) . “0”;
$b = substr($Num,1,1);
$tmp = self::$arrNumber[$a];
$tmp .= ” ” . self::$arrNumber[$b];
}
else if ($Num >=101 && $Num <=999)
{
if ($Num >=101 && $Num <=109)
{
$a = substr($Num,0,1);
$b = substr($Num,1,1);
$c = substr($Num,2,1);
$tmp = self::$arrNumber[$a];
$tmp .= self::$arrNumber[100];
if ($b != 0)
$tmp .= self::$arrNumber[$b];
$tmp .= self::$arrNumber[$c];
}
else if ($Num >=110 && $Num <=120)
{
$a = substr($Num,0,1);
$b = substr($Num,1,2);
$tmp = ” ” . self::$arrNumber[$a];
$tmp .= ” ” . self::$arrNumber[100];
$tmp .= ” ” . self::$arrNumber[$b];
}
else
{
$a = substr($Num,0,1);
$b = substr($Num,1,1) . “0”;
$c = substr($Num,2,1);
$tmp = ” ” . self::$arrNumber[$a];
$tmp .= ” ” . self::$arrNumber[100];

if ($b != ’00’)
$tmp .= ” ” . self::$arrNumber[$b];

if ($c != 0)
$tmp .= ” ” . self::$arrNumber[$c];

}
}
return “$Num = $tmp”;
}
}

?>