Forum Moderators: coopster

Message Too Old, No Replies

If and Else Statements in PHP

         

lindajames

1:57 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



I have written the following code in asp that makes use of simple If and Else

<%
If Request.Form("transStatus") = "Y" Then
Response.Write "<h3>Success</h3> "

Else
Response.Write "<h3>Cancelled ...</h3> "

End If
%>

Can anyone please tell me how the above is done in PHP?

Cheers
Linda

ukgimp

2:05 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might need only 1 = as opposed to 2 ==

<?php
if($transStatus =='Y')
{
print "<h3><Success/h3>";
}
else
{
print "<h3>Cancelled..</h3>";
}
?>

Gibble

2:29 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you need both == I believe, just one will set it and it will always return true

lindajames

2:42 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



It says $transStatus but how do you define $transStatus?

ukgimp

2:45 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats php for you. In the posting of the form you have passed the variable and in php you can just pick it up. The $ delares it so to speak.

You can delare it if you want to but in this case there is no need.

Knowles

2:45 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



If it data from a form being sent it would be defined by the form. Do you know if you have globals turned on or off? (I am guessing off) you would need to add:

$tranStatus = $HTTP_POST_VARS['transStatus'];

I am guessing its coming from a form.

[edited by: Knowles at 2:48 pm (utc) on June 17, 2003]

lindajames

2:47 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Yeap thats correct, its coming from a form that uses the POST method not GET

Knowles

2:48 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Sorry its $HTTP_POST_VARS I had it wrong but edited it to fix.

lindajames

2:50 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Thanx Knowles, I will try that out.

One other question. I dont have SSI support on my site, so ive been told to use the PHP include function to include the text file. can anyone tell me how i can include a file with PHP? in SSI its: <!--#include virtual="notes.dat" -->

Cheers
Linda

Knowles

2:51 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



include = "filename.txt";

that should do ya.

lindajames

2:52 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



But dont i put no tags to start? like <-- Command --> etc.?

Knowles

2:53 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Not as long as its within the

<?php

?>


tags... anything within those tags will be executed as PHP.

[edited by: Knowles at 2:55 pm (utc) on June 17, 2003]

lindajames

2:55 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Can i use the <?php?> tags more than once? for example i execute a PHP command then do a bit of html and then back to PHP

Gibble

2:57 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes you can go back and forth between php and html

Knowles

2:58 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Sorry I had to update my last post. Yes you can mix and match them:

<?php
if else stuff like that
?>
html stuff
<?php
if else stuff like that
?>

lindajames

3:01 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



Thanx Knowles

lindajames

8:17 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



I tried the following:

<?php

$transStatus=$_REQUEST['transStatus'];

if($transStatus =='Y')
{
print "<h3>Success</h3>";
}
else
{
print "<h3>Cancelled..</h3>";
}

?>

But it doesnt seem to work, i even tried changing the if($transStatus =='Y') to a single = but that way it works but it always returns the success message whether or not the transStatus is Y or not.

Any suggestions would be very much appreciated.

Cheers
Linda

lindajames

8:26 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



I've seemed to have got it working by changing it to:

if($transStatus =="Y") instead of if($transStatus =='Y')

I guess it was the " can anyone tell me if that was the actual problem?

Cheers

dingman

9:57 pm on Jun 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn't have been the problem. "Y" and 'Y' should both be interpretted as a string litteral consisting of the single character Y. The only difference is that strings in double quotes will be checked for variable interpolation and escape sequences when they are evaluated, and strings in single quotes are only checked for the escape sequence \', which lets you put a single-quote in a single-quoted string.

Do you perhaps have an editor that tries to do "smart" quotes, where the opening and closing double quotes are different? That might result in characters in your source that aren't quotes at all, as far as PHP is concerned. Otherwise, the problem wasn't the quotes.

Knowles

11:52 pm on Jun 17, 2003 (gmt 0)

10+ Year Member



$transStatus=$_REQUEST['transStatus'];

I would use:
$transStatus=$_POST['transStatus'];

I have never use $_REQUEST so dont know if it would work or not.