Thursday, February 10, 2011

Thursday, 2/10/11


<?php
//example 3-1-4.php

$a = 1;
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";

$a = 1.5;
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";

$a = true;
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";

$a = "Test";
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";

$a = '3.5';
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";

$a = "3.5E27";
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";

$a = "0x19";
echo "is_numeric($a) = " . (is_numeric($a) ? "true" : "false") . "\n";

echo "
";
echo "
";
echo "
";

//example 3-1-5.php

$a = 123;
echo "is_int($a) = " . (is_int($a) ? "true" : "false") . "\n";

$a = '123';
echo "is_int($a) = " . (is_int($a) ? "true" : "false") . "\n";

echo "
";
echo "
";
echo "
";

// example 3-1-11.php

$a=50.3;
$b=50.4;
$c=100.7;

if ($a + $b == $c) {
echo "$a + $b == $c\n";
}
else {
echo "$a + $b != $c\n";
}

echo "
";
echo "
";
echo "
";

for ($i = 0; $i < 100; $i += 0.1) {
if ($i == 50) echo '$i == 50' . "\n";
if ($i >= 50 && $i < 50.1) echo '$i >= 50 && $i < 50.1' . "\n";
}
?>


Next post



<?php
// Example 3-2-8.php
function GeneratePassword($min = 5, $max = 8) {
$ValidChars = "abcdefghijklmnopqrstuvwxyz123456789";
$max_char = strlen($ValidChars) - 1;
$length = mt_rand($min, $max);
$password = "";
for ($i = 0; $i < $length; $i++) {
$password .= $ValidChars[mt_rand(0, $max_char)];
}
return $password;
}

echo "New password = " . GeneratePassword() . "\n";
echo "New Password = " . GeneratePassword() . "\n";
?>


This is a really cool tutorial on how to make a bar graph. I can't say i completely understood it.


<?php
// Example 3-3-4.php
define('BAR_LIN', 1);
define('BAR_LOG', 2);

function ShowChart($arrData, $iType = BAR_LIN, $iHeight = 200) {
echo "<table border=0><tr>";
$max = 0;
foreach($arrData as $y) {
if($iType == BAR_LOG) {
$y = log10($y);
}
if ($y > $max) $max = $y;
}
$scale = $iHeight / $max;

foreach($arrData as $x=>$y) {
if ($iType == BAR_LOG) {
$y = log10($y);
}
$y = (int)($y*$scale);
echo "<td valign=bottom>
<img src='dot.png' width=10 height=$y>
</td>
<td width=5> </td>";
}
echo "</tr></table>";
}

$arrData = array(
150,
5,
200,
8,
170,
50,
3
);

echo '<html><body>';

echo 'show chart with linear scale';
ShowChart($arrData, BAR_LIN);

echo '
Show chart with logarithmic scale';
ShowChart($arrData, BAR_LOG);

echo '</body></html>';

?>

Here is a post on an introduction to arrays.



<?php
$my_array = array();

$pets = array('Tweety', 'Sylvester', 'Bugs', 'Wile E.');

$person = array('Bill', 'Jones', 24, 'CA');
$customer = array('first' => 'Bill', 'last' => 'Jones', 'age' => 24, 'state' => 'CA');

print "

Pet number 1 is named '$pets[0]'.

\n";
print "

The person's age is $person[2].

\n";
print "

The customer's age is {$customer['age1']}.

\n";

?>


Here is a tutorial on multi-dimensional arrays


<?php
$customers = array(
array('first' => 'Bill', 'last' => 'Jones',
'age' => 24, 'state' => 'CA'),
array('first' => 'Mary', 'last' => 'Smith',
'age' => 32, 'state' => 'OH'),
array('first' => 'Joyce', 'last' => 'Johnson',
'age' => 21, 'state' => 'TX'),
);

$pet_breeds = array('dogs' => array('Poodle', 'Terrier', 'Dachshund'),
'birds' => array('Parrot', 'Canary'),
'fish' => array('Guppy', 'Tetra', 'Catfish', 'Angelfish')
);

printf("

The name of the second customer is %s %s.

\n",
$customers[1]['first'], $customers[1]['last']);

printf("

%s and %s

", $pet_breeds['dogs'][0], $pet_breeds['birds'][1]);


?>


The implode() function represents a handy way to output an entire indexed array in one go.


<?php
$languages = array('German', 'French', 'Spanish');

printf("

Languages: %s.

\n", implode(',',$languages));

?>


Here is another tutorial from the book PHP and MySQL Development


<?php
$pictures = array('tire.jpg', 'oil.jpg', 'spark_plug.jpg', 'door.jpg', 'steering_wheel.jpg', 'thermostat.jpg', 'wiper_blade.jpg', 'gasket.jpg', 'brake_pad.jpg');
shuffle($pictures);
?>

<html>
<head>
<title>Bob's Auto Parts - Customer Orders</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<div align="center">
<table width = 100%>
<tr>
<?php
for ($i = 0; $i < 3; $i++) {
echo "<td align=\"center\"><img src=\"";
echo $pictures[$i];
echo "\"/></td>";
}
?>
</tr>
</table>
</div>
</body>

No comments: