Making A PHP variable from 2 other variables

php dynamic variable names in for loop
php variables
php variable in string
php get variable name
php variables types
php static variable
php global variable across files
php global variable in class

I'm starting a magazine which has 3 editions. UK, US, and European. Each will be published online at different times, therefore each has a different issue number.

So I want to create a way of selecting which issue goes with which edition, but I don't know how to join these 2 parts together. The code I'm using is:

$this_edition = "UK";

$UK_issue = "1";
$US_issue = "3";
$EU_issue = "2";

$this_issue = // The Issue Relating to This Edition

What I want to do here is basically call the issue number ( 1/3/2 ) which relates to the $edition I'm udating, in this case, UK.

So essentially : $this_issue = $edition_issue

Does anyone know how I can do this?

What you're trying to do is possible...

$this_issue = ${$this_edition.'_issue'};

But it's way better to use the solution suggested by pr1nc3.

PHP variables, How can I assign one variable to another variable in PHP? You don’t really send variables in php. If you include another php file, the included file will have access to all global variables. If you want to keep the value of some variable between page requests then you will need to store the value somewhere. The easiest places to store values are in cookies, sessions or a database.

How about an array so you have keys and values to match?

For example $magazine = [1 => 'UK', 2 => 'US', 3 => 'EU'];

    [1] => UK
    [2] => US
    [3] => EU

Your array keys (issues) match the country(edition)

Dynamic variable in PHP - Oceanize Geeks, are integer (whole numbers), float (real numbers), strings, and booleans. Passing PHP form variables from one page to other This is in continuation of the PHP multi-page order form article. The first paged explained how to pass the variables between pages using sessions.

You may use Associative Array as shown below.

$this_edition = "UK";
$edition['UK'] = 1;
$edition['US'] = 2;
$edition['EU'] = 3;
$this_issue = $edition[$this_edition];

How to Display PHP Variable Values, PHP allows you to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. That is, one variable contains the name of another variable. For example, suppose you want to construct a variable named $city with the value Los Angeles. If you include/require other files, you don’t need to transfer variables. If you have different requests, like sending a location header, or a link click, ther are two options.

How about an array so you have keys and values to match?

$magazine = array(1 => 'UK', 2 => 'US', 3 => 'EU');
echo $magazine[$issue];

output will be


Variables and Types - Learn PHP, Depending on the current page, I would like to change the css of the chosen menu module and make others different, etc. All that while building  PHP Variables Scope. In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local; global; static

I think you have already got your answer, But just want to show you another way if you need to hold more details about your editions and issues in an array.

    $this_edition = "UK";

    $editions = array("UK"=>array("name"=>"UK Edition","value"=>1), "US"=>array("name"=>"US Edition","value"=>2), "EU"=>array("name"=>"EU Edition","value"=>3));

    echo $this_issue = $editions[$this_edition]["name"];

Output: UK Edition

If you keep your information inside mapped array, you will be able to receive them very easily at any point you need.

How to Use PHP Variable Variables, Creating (Declaring) PHP Variables. In PHP, a variable starts with the $ sign, followed by the name of the variable: and underscores (A-z, 0-9, and _ ); Variable names are case-sensitive ( $age and $AGE are two different variables). A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello , can be used as the name of a variable by using two dollar signs. i.e.

Build a variable name in PHP out of two variables, PHP variable scope: learn all about PHP variables and get main tips on PHP static variable usage in projects. Updated Oct 2, 2019 By expressing a PHP variable outside the function, you make it a PHP Global variable. Note: Different variables with local PHP variable scopes can have identical  I want new variable ( RaceEthnicity) to have 5 categories based on these conditions. If Ethnicity= '1' and Race =Any then RaceAndEthnicity = Hispanic- Any Race. If Ethnicity= '2' and Race =White then RaceAndEthnicity = White NH. If Ethnicity= '2' and Race =Black or African American then RaceAndEthnicity = Black NH.

PHP Variables, How to output text strings and $variables inside PHP code. procces, so i hope this tutorial will give you ideas where to look in your code. But if you inserting variable values inside Single quotes the $var will not parse (php will print and end of css declaration class="myClass" as shown in Example 2-5. Three methods by which you can use variables of one php file in another php file: use session to pass variable from one page to another method: first you have to start if you want to pass variable value using button then u can use it by following method: $x='value1'

Tips on PHP Variable Scope: PHP Variables With Examples Included, PHP variables are used for storing values such as numeric values, character the different capitalization schemes make for different variables. 2, we declared a variable called $v which stores the value 'var1' and in line no. For something seemingly so straight-forward, you would think that the information out there would be a bit better on this subject. Passing a variable from PHP to JavaScript is actually very easy! There are just a couple things you need to keep in mind and some ways to do it to ensure it actually works. I find that putting the following code

  • Using arrays would make more sense imo.
  • Maybe I misunderstood your problem, but isn't it just enough to use $this_edition with the value of $*_issue?
  • Meaning? I basically just want to select the issue number based on the Edition!
  • @maio290, the script isn't always going to be UK. I load that based on the Issue type. I want to say something like $this_issue = $edition_issue.
  • Thanks. I've tested it this way and it's working fine, so I'll go with it. I only know basic PHP so I'd like to keep it simple with something that I understand. Thanks again