Forum Moderators: coopster

Message Too Old, No Replies

if else with in a variable

if else with in a variable

         

iainsmith4

4:29 pm on Feb 15, 2010 (gmt 0)

10+ Year Member



Hi all, new to the forum, can any one offer any advice...

I am trying to create a varibale called $message and assign to that variable a mixture of string and if/ else strings of text for example

$this = "yes";

$message = "hello my name is iain ". if ($this == "yes"){ //do this} else { // do this }."More text here";

So basiclly can I store or run php statement with inside a variable? if so how!

as I get the following error when trying to:

Parse error: syntax error, unexpected T_IF in C:\wamp\www\gofindResponseReports\emailTests\emailV1.php on line 108

Thanks.

Matthew1980

5:06 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there IainSmith4,

Welcome to the forum!

With regards to your question, the easiest way to achieve this is by doing something like this:-


$message = "hello my name is iain ".(isset($this) && ($this == "yes") ? 'do this as its true' : 'do this as its false')."More text here";


Basically, if I have written this correctly ;-p, if the var ($this) is set and its equal to yes, then echo the contents before the colon. If its false echo the contents after the colon.

All that I have done otherwise is concatonate the value into the echoed string, I hope that makes sense!

Disclaimer: I wrote this without testing it, but you should be OK.

Cheers,

MRb

rocknbil

7:38 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You canot use $this as a "regular" variable, it is for usage in classes and functions, referring to the current object.

The ternary operator is your friend. :-) Couldn't get it to work in a concatenate there though, which I don't understand because I've done it before. Anyway . . . .

<?php
header("content-type:text/html");
$thisvar = "yes";
//$thisvar = "no";
echo "hello my name is iain ";
echo ($thisvar=="yes")?" welcome aboard!":" Hey you haven't signed up yet!";
?>

Matthew1980

8:31 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey there rocknbil,

Funny you should mention the issue of the use of $this, it dawned on me a shortly after I had posted that it was a reserved name. I should have amended my post :-p

As for the concatenation, it works fine for me:-


//$string = "yes";
$string = "no";
$message = "hello my name is iain ".(isset($string) && ($string == "yes") ? 'do this as its true' : 'do this as its false')."More text here";

echo $message;


Just that when you said it didn't work, I was curious...

Cheers,

MRb

rocknbil

12:40 am on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<shrug> Mine kept echoing "welcome aboard" in a one liner even when I set $thisvar = no. It should have worked, but was too busy to fuss about it. :-P

iainsmith4

8:51 am on Feb 16, 2010 (gmt 0)

10+ Year Member



Hi all thanks for the warm welcome, and feedback.

I have not as of yet used the scripting in that form, but I will give the above ago and see if I can make sense of it.

Can the same logic be applied to data directly from a recordset for a database,

ie.

Recordset called $rsGetCategory

$message = "hello my name is iain ".(isset($rsGetCategory['image']) && ($rsGetCategory['image'] == "y") ? 'do this as its true' : 'do this as its false')."More text here";

As this is what i will want to achieve, I am trying to build a HTML email with unique content to the each email address stored.

Thanks

Matthew1980

9:19 am on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there IainSmith4,

From the example that you have provided it will do the job that you are asking of it. Unless I'm that tired and havn't noticed anything glaringly obvious ;-)

Good luck coding the rest of the email template too!

Cheers,

MRb

iainsmith4

9:20 am on Feb 16, 2010 (gmt 0)

10+ Year Member



Hi Matthew1980, thanks for your reply I will give this ago this morning and hopefully crack it! thanks for your help :)

iainsmith4

9:42 am on Feb 16, 2010 (gmt 0)

10+ Year Member



Success! I got a working HTML to my email address with data from my database displaying!

Thanks all.