<?php // This comments out a single line /* This comments multiple lines */ ?>
<?php // Print out a string echo "hello world"; ?>
<?php // define a variable $foo = "hello world"; // print the variable echo $foo; ?>
<?php
// define a function
function foo ( $bar )
{
// return a string
return "hello " . $bar;
}
echo foo ("world");
?>
<?php // create an array $foo = array( 'ant', 'bird', 'dog' ); // print the SECOND element echo $foo[1]; ?>
<?php // create an associative array $foo = array( 'a' => 'ant', 'b' => 'bird', 'c' => 'cat', 'd' => 'dog' ); echo $foo['a']; $foo['b'] = 'boy'; echo $foo['b']; ?>
<?php $foo = array( 'a' => array( array( 'ant', ), 'apple' ), 'b' => 'bird', 'c' => 'cat' ); echo $foo['a'][0][0]; $foo['d'] = 'deer'; ?>
<?php
for ($i = 0; $i<5;$i++)
{
echo $i.',';
}
?>
<?php
$foo = array(
'cat',
'dog'
);
foreach ($foo as $bar)
{
echo $bar.',';
}
?>
<?php
$count = 0;
while ($count < 5)
{
echo $count.',';
$count++;
}
?>
<?php $foo = array( 'a' => array( array( 'ant', ), 'apple' ), 'b' => 'bird', 'c' => 'cat' ); print_r($foo); ?>
Array
(
[a] => Array
(
[0] => Array
(
[0] => ant
)
[1] => apple
)
[b] => bird
[c] => cat
)
<?php
try {
$d = 34 / 0;
}
catch (Exception $e) {
echo $e->error();
}
?>
Warning: Division by zero in /home/ddm11/public_html/php_basics.php on line 384
include("filename.php");