How can I do that?
I have something like:
define($stuff.'_FOO', 'whatever');
echo $stuff.'_FOO';
and it doesn't work :(
I just want to echo the constant's value...
Check out constant().
In your case:
echo constant($stuff . '_FOO');
First make a constant:
define("FOO_BAR", "something more");
then you can get the value by using constant():
echo constant("FOO_BAR");
Read more about constants in the manual.
Related
i have a problem like the following sample code :
$code='100'; //$maybe $code='0' or $code='1' ... i just set a number as an sample
$xx=array(
'0'=>array(a,b,c),
'1'=>array(d,e,f),
........
'100'=>array(aa,bb,cc)
);
I want find $code in array :
if($code==$xx['$code']){
echo $xx['code'][0]; //if i want get the value 'aa'
}
But it seems like $xx['$code'] doesn't work.
Do anyone know the right way to solve it?
Firstly you need to use array_key_exists for getting within if condition and then after you can use it simply like as
if(array_key_exists($code,$xx)){
echo $xx[$code][0];
}
or can simply use isset instead like as
if(isset($xx[$code])){
echo $xx[$code][0];
}
you shouldn't use single quotations here,
when filling displaying a variable either use without quotations or put it between double "" try $xx[$code] or $xx["$code"]
Use isset OR !empty to check key exist in array or not. It will also check that key has valid value.
if(isset($xx[$code])){
echo $xx[$code][0];
}
OR
if(!empty($xx[$code])){
echo $xx[$code][0];
}
if($code==$xx['$code']){
echo $xx[$code][0]; //if i want get the value 'aa'
}
If you use '$code' the content of $code will not be checked as '' interpret everything as a string and won't look a variables in it.
You can't sace 'code' either, as code is only the name of the variable you use.
I am trying to check defined variables based on passing a single portion of the variable. (The rest of the variable is static and all other portions of it are the same), so I made a test to find out if this is possible.
It does not work, but perhaps I am doing something small that is easily fixed.
define('TEST', 'works');
$test = 't';
echo TES . strtoupper($test);
echo eval('TES . strtoupper('.$test.');');
echo eval('TES . strtoupper(\'$test\');');
echo eval('TES' . strtoupper($test) . ';');
If you want to check if a constant is defined, simply use defined()
<?php
if (defined('TEST')) {
echo TEST;
}
?>
This should work for you:
Just use constant() to build the constant name as string and then pass it to the function.
echo constant("TES". strtoupper($test));
output:
works
'TEST' is not a global variable. It is a constant. The constants have global scope, they can be accessed from any context (given you know the name of the constant you want to use). There is no need to do any hack using eval().
If you generate the name of the constant on runtime for some reason, you can use the PHP function defined() to check if there is a constant already defined having that name and, if the constant exists, you can get its value using the function constant()
Like this:
define('TEST', 'works');
$test = 't';
echo 'TES'.strtoupper($test);
// Compute the constant's name
$name = 'TES'.strtoupper($test);
// Check if the constant exists and get its value
if (defined($name)) {
echo("The constant '".$name."' is already defined.\n");
echo("It's value is: ".constant($name)."\n");
}
I trying get another variable in php, from existing text but I have issue to do this.
When I echo $address_number; I get list3.
So I need to get variable $list3 and I need echo $list3; after that.
Something like
<?php echo $(echo $address_number;); ?>
But this is not possible. Any solutions?
Solution:
$address_number = $address.$number;
$iframe_url ="$address_number";
<?php echo $$iframe_url; ?>
Thanks everybody!
Use variables variables and the way they work is like so
$adress_number = "list3";
$list3 = "some value";
echo $$address_number;
the above code will output 'some value', what it's doing is using the value of the variable $address_number and treating it as a variable name, so it will look for that variable name and print it's value
I have a simple PHP file with the following:
<?php
echo 'catid=$_GET["catid"]';
<?>
When I run the page, the output is:
catid=$_GET["catid"]
I'm accessing the page as www.abc.com/temp.php?catid=3. I'd like $_GET to execute so I see:
catid=3
What am I doing wrong?
You have to cancat the two:
echo 'catid=' . $_GET["catid"];
or you could use " (double quotes):
echo "catId=$someVar";
$_Get is a variable, and to echo a variable you do not need parenthesis around it.
<?php
echo 'catid='.$_GET["catid"];
?>
please see this : source
You can use non-array variables for that:
$getCatID = $_GET["catid"];
echo "catid=$getCatID";
Or you can use (recommended):
echo 'catid=' . $_GET["catid"];
You may try:
echo "catid= {$_GET['catid']}";
The best way to use variables in string is:
echo "catid={$_GET['catid']}";
You have to use double quoted string to notify PHP that it might contains variables inside. $_GET is array, so you will need to put the variable statement in {}.
<?php
echo "catid={$_GET['catid']}";
?>
There are a few options to combine a variable with a string.
<?php
$var = "something";
// prints some something
echo 'some ' . $var; // I prefer to go for this one
// prints some something
echo "some $var";
// prints some $var
echo 'some $var';
// prints some something
echo "some {$var}";
?>
I have a variable defined using define()
I want to store a part of the name of that variable in a regular php $variable,
then access that defined variable by setting the name dynamically. ie.:
define('xxx_yyy',123);
$a='xxz';
$b='_yyy';
//How to I echo out "123" now? (without using echo xxx_yyy);
//Something like these (don't work):
echo $a$b;
echo {$a$b};
The only thing I can think of is:
$defined=get_defined_vars();
echo $defined[$a$b];
but that seems clunky
echo constant ( $a . $b );
is what i think you are looking for as it is a constant.
You can use constant(). http://us3.php.net/manual/en/function.constant.php
It's not a variable, it's a constant:
echo constant ( $a . $b );
The correct function for this is get_defined_constants() and not get_defined_vars().
To make 123 echo out, use this:
echo $a . $b;
Also, define() makes constants not variables.