Beginning PHP:
working example description source code [and annotation] ~ddailey/php/one.php hello world. <?php
print "Hello World!";
?>~ddailey/php/two.php mixing HTML and PHP script <html><body><b>
<?php
$a=3;
$b=7;
echo "Hello World!<br>";
echo "$a plus $b is:";
echo $a + $b;
?></b></body></html>~ddailey/php/three.php another way to print <html><body><b>
<?
$a=3;$b=7;
$c=$a+$b;
print "Hello World!<br>";
print "<br> ".$a." plus ";
print $b." is: ".$c;
?></b></body></html>~ddailey/php/four.php?a=5&b=6 sending parameters through the URL <html><body>
<?
$a=$_GET["a"];
$b=$_GET["b"];
$c=$a+$b;
print "Hello World!<br>";
print "<br> ".$a." plus ";
print $b." is: ".$c;
?></body></html>~ddailey/php/five.php Addition on the server. Combining JavaScript, HTML and PHP. <html><script>
var b="http://granite.sru.edu/~ddailey/php/";
var f="five.php?a=";
var u=b+f;
function send(){
u+=document.f.q.value;
window.location=u;
}
</script><body><?php
$a=$_GET["a"];
$c=$a+1;
print "A counter:<br>";
print "<br>click number: ".$c;
?>
<form name='f'><input name="q" value=
<?php
print $c;
?>
type=hidden>
<input type=button value='again'
onclick='send();'>~ddailey/php/six.php The same thing, a bit simpler? <html><body>
A counter<br>click number:
<? $a=$_GET["a"];if ( $a==0 ) {$a=1;} print $a; ?>
<form name='f'><input name="q" value=
<?php print $a; ?> type=hidden>
<input type=button value='again' onclick="
<?
$a=$a+1;
$b="http://granite.sru.edu/~ddailey/php/";
$f="six.php?a=";
$u=$b.$f;
print "window.location='$b$f'+";
print $a;
?>;"></body></html>~ddailey/php/seven.php Simpler still? Note use of isset()to determine if $b has a value or not
<html><body>
<?php
$b=$_GET["b"];
if (!isset($b)) $b=0;
$c=$b+1;
//$a=$REMOTE_ADDR;
$a=$_SERVER['REMOTE_ADDR'];
print $c." is current value of q<br>from".$a;
?>
<form name='f' action="seven.php" method=GET>
<input name="b" type=hidden
<?php print "value=".$c; ?>
>
<input type=submit value='renew'>
</form></body></html>~ddailey/php/eight.php a wee bit of arithmetic; and a file include. <html>
<body onload="document.forms[0].elements[0].focus();">
<div align="center"><b>Simple game</b><br>
Pick a number from 1 to 20</div>
You are using <? echo $_SERVER['HTTP_USER_AGENT'] ?><br>
<form>type your guess here:<input name="guess"></form>
<blockquote>then press "Enter"<br>
<?
$guess=$_GET["guess"];
$num=floor(20*rand()/getrandmax());
print "correct number is $num <br>";
if (isset($guess)){
print "your guess, $guess is ";
if ($guess < $num) print "too low.";
elseif ($guess > $num) print "too big.";
else print "correct";
}
?>
</blockquote><form><input size=80 value='
<? include "testing";?>'>
</form></body></html>~ddailey/php/nine.php taking strings apart <html><body>
<div align="center"><b>various tricks</b><br>
strings, arrays, other stuff
</div>
You are using <? echo $HTTP_USER_AGENT ?><br>
<form><input size=80 value='
<? include "testing";?>'>
<textarea cols=30 rows=13>
<?
$r=$REMOTE_ADDR;
print "$r is your IP\n";
for ($i=0;$i<strlen($r);$i++){
print "$r[$i]\n";
}
?>
</textarea>
</form>
</body></html>