Jump to content

Quick Question


Unholy Prayer

Recommended Posts

Ok, I'm making an affiliates management program and I want to give the user the option to display their banners either vertically or horizontally.  Would it be possible to have an if statement inside of a string?  Would something like this work?

 

$display = if($banner_display == horizontal)... 
                   {
                        echo "  ";
}

 

The ... means that there's more but don't feel like writing it.  Would that work?

Link to comment
https://forums.phpfreaks.com/topic/38707-quick-question/
Share on other sites

Well this doesn't work..

 

<?php

$banner_display = "horizontal";

$display = if($banner_display == "horizontal") {
echo "Horizontal";
}

echo $display;

?>

 

Parse error: syntax error, unexpected T_IF

 

 

You can accomplish that with a function.

 

<?php

function DisplayType() {

$display = "vertical";

  if ($display == "horizontal") { 
  	echo "Horizontal";
  }
  if ($display == "vertical") {
  	echo "Vertical";
  }

}

DisplayType();

?>

Link to comment
https://forums.phpfreaks.com/topic/38707-quick-question/#findComment-185943
Share on other sites

Should have paid more attention before...you aren't assigning a value to $display using your first code example...

 

if you change it to this:

 

<?php
$banner_display = "horizontal";

$display = ($banner_display == "horizontal") ? "Horizontal" : "Vertical";

echo $display;
?>

 

It will work.  Look in the manual for ternary operators.

Link to comment
https://forums.phpfreaks.com/topic/38707-quick-question/#findComment-185948
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.