Question regarding if statement

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!

Moderator: General Moderators

Post Reply
geraldwhite
Forum Newbie
Posts: 2
Joined: Tue Jan 08, 2008 9:52 pm

Question regarding if statement

Post 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.
Last edited by geraldwhite on Thu Jan 10, 2008 7:42 pm, edited 1 time in total.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post 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"; }
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Descriptive Subjects

Post 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.
;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
geraldwhite
Forum Newbie
Posts: 2
Joined: Tue Jan 08, 2008 9:52 pm

Re: Real Newb

Post 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>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Question regarding if statement

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