Jump to content

[SOLVED] Variable inside a variable


goocharlton

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

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.