mcmuney Posted February 15, 2011 Share Posted February 15, 2011 Example URL: abc.php?z=1 (z would be 1, 2 o3 3 in this example) PHP: $x1="example 1"; $x2="example 2"; $x3="example 3"; I'd like to use a hard coded "x" and echo the z from the url and combine it to to echo $xz. In this example, xz=x1, so the echo would display "example 1". How can I achieve this without using a bunch if if/then statements? Link to comment https://forums.phpfreaks.com/topic/227802-indirect-function-using-php/ Share on other sites More sharing options...
DeadxBeat Posted February 15, 2011 Share Posted February 15, 2011 Use an array to store your data. $z = $_GET['z']; // Get z variable from URL $x = array("Example 1", "Example 2", "Example 3"); // Set array(you should probably do this dynamically later on) echo "x[0] = " . $x[0]; // Will display Example 1 echo "<br />"; // Line break for clarity echo "x[1] = " . $x[1]; // Will display Example 2 echo "<br />"; echo "x[2] = " . $x[2]; // Will display Example 3 Link to comment https://forums.phpfreaks.com/topic/227802-indirect-function-using-php/#findComment-1174725 Share on other sites More sharing options...
mcmuney Posted February 15, 2011 Author Share Posted February 15, 2011 I see that you're using the get function to get the z value from the url, but I don't see the $z in your echo??? Use an array to store your data. $z = $_GET['z']; // Get z variable from URL $x = array("Example 1", "Example 2", "Example 3"); // Set array(you should probably do this dynamically later on) echo "x[0] = " . $x[0]; // Will display Example 1 echo "<br />"; // Line break for clarity echo "x[1] = " . $x[1]; // Will display Example 2 echo "<br />"; echo "x[2] = " . $x[2]; // Will display Example 3 Link to comment https://forums.phpfreaks.com/topic/227802-indirect-function-using-php/#findComment-1174728 Share on other sites More sharing options...
DeadxBeat Posted February 16, 2011 Share Posted February 16, 2011 So sorry, overlooked that $z = $_GET['z']; // Get z variable from URL $x = array("Example 1", "Example 2", "Example 3"); // Set array(you should probably do this dynamically later on) echo "x[z] = " . $x[$z]; // Will display whatever z is echo "<br />"l echo "x[0] = " . $x[0]; // Will display Example 1 echo "<br />"; // Line break for clarity echo "x[1] = " . $x[1]; // Will display Example 2 echo "<br />"; echo "x[2] = " . $x[2]; // Will display Example 3 Please understand that this is just the logic of the code, you will want to check to see if $_GET['z'] is set and check to see that the index "z" is in the array before echo'ing it out. Link to comment https://forums.phpfreaks.com/topic/227802-indirect-function-using-php/#findComment-1174750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.