Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JelvisChan

macrumors member
Original poster
Jan 9, 2009
77
0
PHP SCRIPTING HELP, PLEASE:

Does anyone know how to put a PHP if statement in a PHP variable?

Is it like this?
EXAMPLE:

<?php
$variable = $_POST['name'];
$if = if ($variable == 'MamaBear'){
echo 'You are married to PapaBear';
}
?>

<html>
<p><b>'.$if.'</b></p>
</html>



Gracias!
 

&Ingonyama

macrumors member
May 6, 2008
69
0
You can't, try:

PHP:
if ($variable == 'MammaBear')
{
    $text = 'You are married to PapaBear';
}
Code:
<html>
<p><b><?php echo $text; ?></b></p>
</html>

You could write it as a ternary statement too:

PHP:
<?php
$variable = 'MamaBear';
$text = ($variable == 'MamaBear') ? 'You are married to PapaBear' : '';
echo $text;
?>
 

JelvisChan

macrumors member
Original poster
Jan 9, 2009
77
0
Thanks for the reply.
I am starting to get it going, but I need your help a little more:

$content='
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>'.$subject.'</title>
<meta content="text/html; charset=windows-1250">
<link href="msg/style.css" type="text/css" rel="stylesheet">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<script language="Javascript" src="msg/javascript.js"><!--
//-->
</script>
</head>
<body>
';
$content.=$header;


You have this $content variable that gives an html header, then you start posting things:

$content.='

<div align="center"><center>
<table border="0" width="95%"><tr>
<td>

<p align="center"><a href="#new">Post a reply</a> ||
<hr>
<p align="center"><b>'.$subject.'</b></p>
<br>';


Since you have the html headers, you cannot just start adding PHP, which is what I want to do. I want to post an image, only if the user types in a certain name in the form on the page. I've tried this:

Before the HTML, I wrote:

$image = $_POST['name'];
$echo = ($image == 'Hello') ? ' <img src=\"http://images.macrumors.com/vb/images/statusicon/user_online.gif\">' : ''; }


and then I wrote below in the HTML:

echo $echo;

Would this work?

Fill free to provide some other scripts.

Jeff
 

jaikob

macrumors 6502
Jul 1, 2008
429
0
Freeland, MI
Or you could do this:

PHP:
<?php
$if = NULL;
$variable = $_POST['name'];

if($variable == 'MamaBear') {
$if = "You are married to PapaBear";
}

?>
 

Stampyhead

macrumors 68020
Sep 3, 2004
2,294
30
London, UK
Be careful using PHP function words as variable names. This could cause errors to come up. Best to stay away from using words like 'if', 'echo', etc. as variables.
 

OutThere

macrumors 603
Dec 19, 2002
5,730
3
NYC
Thanks for the reply.
I am starting to get it going, but I need your help a little more:

$content='
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>'.$subject.'</title>
<meta content="text/html; charset=windows-1250">
<link href="msg/style.css" type="text/css" rel="stylesheet">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<script language="Javascript" src="msg/javascript.js"><!--
//-->
</script>
</head>
<body>
';
$content.=$header;


You have this $content variable that gives an html header, then you start posting things:

$content.='

<div align="center"><center>
<table border="0" width="95%"><tr>
<td>

<p align="center"><a href="#new">Post a reply</a> ||
<hr>
<p align="center"><b>'.$subject.'</b></p>
<br>';


Since you have the html headers, you cannot just start adding PHP, which is what I want to do. I want to post an image, only if the user types in a certain name in the form on the page. I've tried this:

Before the HTML, I wrote:

$image = $_POST['name'];
$echo = ($image == 'Hello') ? ' <img src=\"http://images.macrumors.com/vb/images/statusicon/user_online.gif\">' : ''; }


and then I wrote below in the HTML:

echo $echo;

Would this work?

Fill free to provide some other scripts.

Jeff

You can incorporate segments of php anywhere within your html...so you could do something like this:

PHP:
<html>
<head>
<title>
Foo
</title>
</head>
<body>
This is my page!
<br>
<?php
if ($_POST['name'] == "Bob") {
echo "<img src=\"./bob.jpg\"><br>Hi Bob!";
}
?>
</body>
</html>

This would check if the user had put the name Bob in the name form on the previous page, and display a picture and say hi if they had.

You should probably read a php tutorial as it looks like you're kind of starting from scratch. I used the tizag one (http://www.tizag.com/phpT/) when I was learning.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.