PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<html>
<head></head>
<body>
<?php
$correctage = 21;
$age = $_POST['age'];
if ($age >= $correctage) {
echo 'Come on in, we have alcohol and girls awaiting you';
}
if ($age < $correctage) {
echo "You're too young for this club, come back when you're $correctage";
}
if ($age > 46) {
echo 'old';
}
?>
</body>
</html>
Heres my question what if I wanted to add another condision where if the person was over 45 they would be too old???
I have no idea how to do this, i'm reading alot but this is bugging me.
thanks.
Last edited by geraldwhite on Thu Jan 10, 2008 7:42 pm, edited 1 time in total.
$correctage = "21";
$age = $_POST['age'];
if ($age > '46') {
echo 'Your old, go home!';}
elseif ($age >= $correctage) {
echo 'Come on in, we have alcohol and girls awaiting you';}
else {
echo "You're too young for this club, come back when you're $correctage"; }
<html>
<head></head>
<body>
<?php
// Set a value... no quotes since it is an integer
$correctage = 21;
// Reality check, what if there is no POST
//$age = $_POST['age'];
$age = isset($_POST['age']) ? intval($_POST['age']) : 0; // look up ternary operator at php.net
// Let run the conditionals
if ($age > 65) {
echo 'Damn. Come on dude, you are way too old';
} elseif ($age >= $correctage) {
echo 'Come on in, we have alcohol and girls awaiting you';
} else {
echo "You're too young for this club, come back when you're $correctage";
}
?>
</body>
</html>
$age = 0;
if (isset($_POST['age'])) {
$age = intval($_POST['age']);
}
You'll still need to understand isset() (which checks if a variable is set) and intval() (which gets a value as an integer, regardless of whether it's a string, etc.).
SuperD offered a better, n00b example than mine. As you are learning still I would go with his suggestion. As things start to come a little more naturally, then you can start tinkering with ternaries and the what not.
I was home yesterday with no internet and I was forced to figure this out on my own, as it was driving me crazy.. took about an hour! Anyway I will look at everything posted above, thanks again guys!
<html>
<head></head>
<body>
<?php
$correct_age = 21;
$over_age = 45;
$age = $_POST['age'];
if( ($age >= $correct_age) && ($age <= $over_age) ) {
echo "Come in. We have girls and beer!";
}
if($age < $correct_age) {
echo "You are too you young for this club.";
}
if($age > $over_age) {
echo "You are too old. Go away!";
}
?>
</body>
</html>
<html>
<head></head>
<body>
<?php
$legal = 21;
$oldfart = 45;
$age = 0;
// Check to see if there is a posted age
if (isset($_POST['age']) {
$age = intval($_POST['age']);
}
// See if we are between legal and old
if ($age >= $legal && $age <= $oldfart) {
echo "Come in. We have girls and beer!";
} else {
// We are either too young or too old
if ($age < $legal) {
echo "You are too you young for this club.";
} else {
echo "You are too old. Go away!";
}
}
?>
</body>
</html>