Jump to content

Recommended Posts

I am referring to this snippet of code:

<?php
if ('$left_column_' . $page_name == 0)
	  echo "";
else
	   include_once ("left_column.php");
?>

 

The problem I am having is in the second line of this code

if ('$left_column_' . $page_name == 0)

. I am trying to check if $left... has a 0 or not but I dont know how to include the $page_name variable inside the other variable.

 

For example this is the complete code that I need to be functional:

<?php
$page_name = "Home";
$left_column_Home = "1";
if ('$left_column_' . $page_name == 0)
	  echo "";
else
	include_once ("left_column.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/59715-solved-variable-inside-a-variable/
Share on other sites

Your confusing yourself.

 

<?php
$pages = array("left_column" => array("home" => 1, "vacation" => 0), "right_column" => array("home" => 0));

if ($pages['left_column']['home'] == 0)
	  echo "";
else
	include_once ("left_column.php");
?>

 

Something like that would be better.

I think you're after the variable variable:

 

<?php
$page_name = "Home";
$left_column_Home = "1";
$var = 'left_column_'.$page_name;

if ($$var == 0)
echo "";
else
include_once ("left_column.php");

 

Edit: Or ,as frost says, you could use arrays.

Thanks for your quick responses especially sasa, I used some of your idea your idea.

I need the code in this.....

<?php
if (${'left_column_' . $page_name} == 0)
?>

 

....way because the 'if' function is in a template and the '$page_name' is at the top of that template page(which will have to be changes according to every page) but the '$left_column_Home' is in a separate site configuration file. I call that with include_once

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.