Using eval() to check if constant isset - php

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");
}

Related

PHP variable not working when referenced with '$'

I'm trying to convert a PHP variable to a JS variable using a little helper function that uses variable variables. To simplify, here is what I'm trying to accomplish:
$project_key = 'project 1';
function to_js($variable) {
echo $$variable;
}
to_js('$project_key');
this is supposed simply print
project 1
instead i get
Undefined variable: $project_key
which tells me the variable is being targeted but can't be accessed from the function. How can I access the global var $project_key from within the function if supplied only with the string $project_key?
Omit the leading $ from $project_key in the following line:
to_js('$project_key');
It should be:
to_js('project_key');
The $ in a variable is not part of the variables name, so you don't need to include it when referencing it in a variable variable.
Remove first $ sign before $variable. If you use $$ the project 1 will be considered as a variable but that is not defined as a variable.
$project_key = 'project 1';
function to_js($variable) {
echo $variable;
}
to_js($project_key);
Reference of $$
Try echoing your variable with script tags around it.
echo "<script>var x =" . $variable . "</script>";
$variable - being the variable you have stored in php
x - being the variable you want to be stored in Javascript.
Try removing the quotes in:
to_js('$project_key');
To be to_js($project_key); as if you use it as to_js('$project_key'); then you set the $variable in the function to the text: '$project_key'.
Wrong answer!#mehedi-pstu2k9 answer's is correct
Reference of $$
See:
https://stackoverflow.com/a/4169891/4977144

how to access php defined constant using a $variable as the name of the defined constant

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.

How to access constant variables (defined in another file) dynamically in a function?

I have a file that defines constant variables, like this:
define_vars.php
<?
define("something","value");
define("something1","value");
define("something2","value");
define("something3","value");
And I have a function which parses $var as the constant variable name, like this:
function something($var=''){
include('define_vars.php');
// $var is the name of one of the variables I am defining in the other file (define_vars.php)
// So $var may have a value of "something3", which is the name of one of the constants defined in the other file...
}
I need to somehow get the value of the constant, when $var holds the name of the constant I wish to get the value of....make sense? :S
Any Ideas?
http://php.net/constant
function something($var) {
if (defined($var)) {
$value = constant($var);
}
}
Also you should make sure that the file with the definitions gets included only once, so use require_once('define_vars.php'); instead.
You want constant()
constant($var); // value
Use constant() to get the value. You could do something like this
function something($var = '') {
include_once('define_vars.php'); //you don't want to include the file more than once or it will cause a fatal error when trying to redefine your constants
return (defined($var) ? constant($var) : null);
}

Checking if a constant is empty

Why is this not possible?
if(!empty( _MY_CONST)){
...
But yet this is:
$my_const = _MY_CONST;
if(!empty($my_const)){
...
define( 'QUOTA_MSG' , '' ); // There is currently no message to show
$message = QUOTA_MSG;
if(!empty($message)){
echo $message;
}
I just wanted to make it a little cleaner by just referencing the constant itself.
See the manual: empty() is a language construct, not a function.
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
So you'll have to use a variable - empty() is really what you want in the first place? It would return true when the constant's value is "0" for example.
Maybe you need to test for the existence of the constant using defined() instead?
Just letting you know that you can do
if(!empty(MY_CONST))
since PHP 5.5.
You can get along with this if for some reason you are still not using PHP 5.5.
if (defined('MY_CONST') && MY_CONST) {
echo 'OK';
}
if (!empty(constant('MY_CONST')) {
...
}
mixed constant ( string $name )
Return the value of the constant indicated by $name, or NULL if the constant is not defined
http://php.net/manual/en/function.constant.php
what about strlen?
if(strlen(MY_CONST) == 0)
If constant defined and need check is empty or not, for example you use it in config file, you can use !MY_CONST:
define('MY_CONST', '');
if (!MY_CONST) {
echo 'MY_CONST is empty';
}

What does & sign mean in front of a variable?

I'm 'dissecting' PunBB, and one of its functions checks the structure of BBCode tags and fix simple mistakes where possible:
function preparse_tags($text, &$errors, $is_signature = false)
What does the & in front of the $error variable mean?
It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.
function passByReference(&$test) {
$test = "Changed!";
}
function passByValue($test) {
$test = "a change here will not affect the original variable";
}
$test = 'Unchanged';
echo $test . PHP_EOL;
passByValue($test);
echo $test . PHP_EOL;
passByReference($test);
echo $test . PHP_EOL;
Output:
Unchanged
Unchanged
Changed!
It does pass by reference rather than pass by value.
This allows for the function to change variables outside of its own scope, in the scope of the calling function.
For instance:
function addOne( &$val ) {
$val++;
}
$a = 1;
addOne($a);
echo $a; // Will echo '2'.
In the case of the preparse_tags function, it allows the function to return the parsed tags, but allow the calling parent to get any errors without having to check the format/type of the returned value.
It accepts a reference to a variable as the parameter.
This means that any changes that the function makes to the parameter (eg, $errors = "Error!") will affect the variable passed by the calling function.
It means that the variable passed in the errors position will be modified by the called function. See this for a detailed look.

Categories