Calling Variables In A Variable


#1

Hi All

It has been a very long time since I have done any coding and am trying to do something that I think may have changed.

I have a variable with a pile of text in it... in that text there are variables that need to be entered depending on the if else statements.

EXAMPLE

$examplecontent = 'It is ' . $cost . ' to buy this in ' . $currency . ' to ship to your ' . $country . '.';

if ($countrycode == "AU")
 {

$cost = "$1";
$currency = "AUD";
$country = "Australia";

echo (' ' . $examplecontent . '');

}

if ($countrycode == "UK")
 {

$cost = "£1";
$currency = "GBP";
$country = "United Kingdom";

echo (' ' . $examplecontent . '');

}

if ($countrycode == "US")
 {

$cost = "$1";
$currency = "USD";
$country = "United States of America";

echo (' ' . $examplecontent . '');

}

and so on.

I cannot get the variables to call into the echoed variable.

I am sure this is an easy fix but having troubles.

Any help would be great.

Thanks

mrmbarnes


#2

Try setting error_reporting(-1); and ini_set('display_errors', 'true'); at the top of the script.

It should show a syntax error with the " in the echo statement.

Edit:
Corrected spelling mistake.


#3

Thanks for that... the errors are that the variables are not defined yet, as per the above example, it is saying that the $cost, $currency and $country variables have not been set yet.

Any idea how to fix this?


#4

Before setting ‘$exampleContent‘ set the the variable to something like $cost = 'default';

The variables should be over written after the if tests.


#5

Good idea but nothing is being overwritten... essentially it is reading:

It is to buy this in to ship to your.

Any other ideas?


#6

The above statement is unnecessarily repeated and can be omitted until after all the tests.

Also before and after the tests echo each and every variable followed by die;

Ensure every variable is what is expected. Continue until problem is resolved.

Don't forget to keep error reporting and showing all errors and warnings.


#7

There are no errors but it is not working the way I want it.

I have added default variables at the top and it is just pulling in that info but it is pulling the correct if else statements.

This should be working, I think, but it isn't... anyone else have any ideas?


#8

Can you try forcing the $country code. also trying again with blank spaces before and after $countrycode?

Also try var_dunp( $countrycode );

It is also easier to add line numbers to displayed variables.

echo '<br>' .__line__ .' ==> '  .$countrycode; die;

#9

Back on the desktop:

<?php 
declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', 'true');

# DEFAULTS
  $cost         = 'DEFAULT: 001'; 
  $currency     = 'DEFAULT: 002'; 
  $country      = 'DEFAULT: 003'; 
  $countrycode  = '   UK ';

$examplecontent = 'It is ' 
                . $cost 
                . ' to buy this in ' 
                . $currency 
                . ' to ship to your ' 
                . $country . '.';

echo  '<br>BEFORE: '
      .'<br>$code ==> '       .$cost
      .'<br>$currency ==> '   .$currency
      .'<br>$country ==> '    .$country
      .'<br>$countrycode ==> ' .$countrycode
      .'<br>$examplecontent ==> ' .$examplecontent;

# REMOVE POSSIBLE SPACES
  $countrycode  = trim($countrycode);

if ($countrycode == "AU")
{
  $cost = "$1";
  $currency = "AUD";
  $country = "Australia";
  # echo (' ' . $examplecontent . '');
}


if ($countrycode == "UK")
{
  $cost = "£1";
  $currency = "GBP";
  $country = "United Kingdom";
  # echo (' ' . $examplecontent . '');
}

if ($countrycode == "US")
{
  $cost = "$1";
  $currency = "USD";
  $country = "United States of America";
  # echo (' ' . $examplecontent . '');
}

# FINISHED
  $examplecontent = 'It is ' 
                . $cost 
                . ' to buy this in ' 
                . $currency 
                . ' to ship to your ' 
                . $country . '.';

echo  '<br><br>AFTER: '
      .'<br>$code ==> '       .$cost
      .'<br>$currency ==> '   .$currency
      .'<br>$country ==> '    .$country
      .'<br>$countrycode ==> ' .$countrycode
      .'<br>$examplecontent ==> ' .$examplecontent;



Output:

BEFORE:
$code ==> DEFAULT: 001
$currency ==> DEFAULT: 002
$country ==> DEFAULT: 003
$countrycode ==> UK
$examplecontent ==> It is DEFAULT: 001 to buy this in DEFAULT: 002 to ship to your DEFAULT: 003.

AFTER:
$code ==> £1
$currency ==> GBP
$country ==> United Kingdom
$countrycode ==> UK
$examplecontent ==> It is £1 to buy this in GBP to ship to your United Kingdom.



Edit:
Added $examplecontent and amended defaults.


#10

Thanks for all of that... from looking at that example I realised all I needed to do was move my code from the top to the bottom and now it works.


#11

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.