Page 1 of 1

Question regarding if statement

Posted: Tue Jan 08, 2008 9:57 pm
by geraldwhite
Ok I have no idea what I'm doing here, but I have a question I have been doing php for 1 day. see my code below.

Code: Select all

<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.

Posted: Tue Jan 08, 2008 11:03 pm
by me!
You need an elseif

This will work for you:

Code: Select all

$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"; }

Posted: Wed Jan 09, 2008 12:50 am
by RobertGonzalez
See comments...

Code: Select all

<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>

Posted: Wed Jan 09, 2008 7:19 am
by superdezign
Everah wrote:See comments...

Code: Select all

//$age = $_POST['age'];
$age = isset($_POST['age']) ? intval($_POST['age']) : 0; // look up ternary operator at php.net
Or, even more beginner-friendly:

Code: Select all

$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.).

Descriptive Subjects

Posted: Wed Jan 09, 2008 9:54 am
by pickle
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
;)

Posted: Wed Jan 09, 2008 10:26 am
by RobertGonzalez
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.

Re: Real Newb

Posted: Thu Jan 10, 2008 7:40 pm
by geraldwhite
ok you guys rule!!!

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!


here is the code I came up with to make it work.

Code: Select all

<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>

Re: Question regarding if statement

Posted: Thu Jan 10, 2008 9:55 pm
by RobertGonzalez
You could get away with something like that. You could also do something like:

Code: Select all

<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>