Basic PHP functions


Great site where all the PHP basics such as syntax, variables, classes, pointers, object oriented stuff, file I/O, and various functions are outlined: http://www.astro.ufl.edu/~warner/prog/php.html

Also, please visit this for pre-defined functions in PHP to use: http://php.about.com/od/phpfunctions/

Example of a simple Guess My Number PHP program:

<!--Here is the code for Guess My Number in PHP, a program that generates a random number between 1 and 100 and asks the user to guess it.
It will tell the user if the number is higher or lower after each guess.
This is just one way to write it in PHP. -->

<head>
<title>Guess My Number (PHP)</title>
</head>
<body>
<h1>Guess My Number (PHP)</h1>

<form method="post" action="<?= $_SERVER['PHP_SELF'];?>">
Guess My Number (1-100): <input type="text" id="guess" name="guess" size=3>
<br>
<input type="submit" value="Guess" name="Guess_Button">

<p>Result:
<!-- php code goes here -->
<?php
if(isset($_POST['number']))
{
	$y = $_POST['number'];
}
else
{
	$y = rand(1,100);
}
if(isset($_POST['guess']))
{
$x = $_POST['guess'];
$value = $_POST['number'];//retrieve the random value from the hidden html element and assign to a variable

if($x < $value)
{
	echo $x." is wrong, guess Higher";
}
if($x > $value)
{
	echo $x." is wrong, guess Lower";
}
if($x == $value)
{
	echo $value." is Correct!";
}
}
?>
<!-- we use the hidden html element to store the random value -->
<input name="number" type="hidden" value="<?= $y ?>"/>
</form>
</p>
<br>

<a href="gmn.php">Play Again</a>

</body>

Advertisements

2 thoughts on “Basic PHP functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s