Jump to content

Recommended Posts

hi i am having a little trouble with an if else

statement

 

basically in my forum type application

 

is this sort of format with an elseif statement ok?

as i keep opening and closing the the php statements

 

 

basically depending on what state $t is at will determine whether a form or message is presented to the user.

 

 
$t = $_GET['topic'];

if ($t == 0)
{	
?>
//a html form goes here
<?php
} #end of if 

elseif ($t == 'c')
{
//code
?>
//A MESSAGE IS DISPLAYED HERE INSTEAD OF A FORM
<?php
}
else
{
//JUST A DEFAULT MSG OR SOMETHING
}
?>
end of html code and page

 

what's happening at the moment is that depending on the status of '$t' it is showing only the first if statement

 

any help is appreciated thanks

 

MazTrin  :)

Your first test is comparing $t with a numeric value

If the value of $t is a string, PHP will convert it to a numeric (integer) so that it can do that comparison.

If $t contains plain text, this conversion will result in it being seen as a numeric 0 by the comparison...

therefore, the first if test is comparing 0 with 0, which matches

 

To demonstrate how PHP tries doing this conversion

try doing

 

echo 0 + "Hello";

echo 1 + "Hello";

echo 0 + "3rd Test";

 

To resolve this, you need to make the first test a string to string test:

if ($t == '0')

so that there is no need for PHP to try and convert $t to a numeric value

ah well i have tried this

try it with two if statements if the first doesnt return anything the second will try changing elseif for if

 

looks fine to me too but try what i suggested

 

 

but it didn't work at all so i tried testing the first if statement and commented out the rest.

and i noticed that it didn't even work...so its probably not the elseif statement at all.

 

so do you think that maybe the get isn't getting the topic

 

my adapted code:

<?php

$t = $_GET['topic'];

if ($t == '0') 
{	
?>
//the code that i want it do display
<?php
} 
else
{
echo 'something went so wrong';
}

when i use this it goes straight to the else statement.

Most likely, if it does not work, then no. The get is not getting the id.

 

<?php

$t = isset($_GET['topic'])?(int)$_GET['topic']:null;

if (isset($t) {
    echo "{$t} was passed in, yay!";
}else {
    echo "Get data was not appened to the url like http://yoursite.com/script.php?t=2";
}
?>

 

Give that a try and see what happens.

Have you tried echoing the value of $t in a few places. i.e.

 

<?php

 

$t = $_GET['topic'];

echo $t;

 

and later...

 

if ($t == '0')

{

echo $t;

}

 

 

I have exactly the same setup, except I dont close the php section and throw in html code - I just converted the html into php echo commands, so that way each if statement has a solid section of php between the { and } - I just prefer it that way lol

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.