Jump to content

Indirect Function using PHP


mcmuney

Recommended Posts

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

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

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

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.

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.