Undefined Constants - php

This worked fine for me on my old server but I have moved to a new server with a more recent version of php and I am getting the following error
Notice: Use of undefined constant company_name - assumed 'company_name' in
How do I define the result as a constant in code below?
$q1 = mysql_query("SELECT company_name FROM company");
print "<ul class='mktree'>"; //open list1
while($nt=mysql_fetch_array($q1)){
print "<li><span>Company - $nt[company_name]</span></li>";
$comp = $nt[company_name];

You should quote your array keys.So
$nt[company_name] must be $nt['company_name']
When you use it without quotes it will look for constants called company_name. When it doesn't find such a constant, PHP (bizarrely) interprets it as a string ('company_name'). Obviously, this can easily break if you do defined such a constant later (though it's bad style to have lower-case constants).

You have to quote the index name.
$nt['company_name']
or company_name will be a undefined constant, and php will assume the value is 'company_name', then trigger a Notice.

You missed out quotes in print $nt[company_name] and $comp = $nt[company_name];
Try with
print "<li><span>Company - $nt['company_name']</span></li>";
$comp = $nt[company_name];
Also if you want to echo the value of $nt[company_name], you have to append the value like:
print "<li><span>Company - ".$nt['company_name']."</span></li>";

Related

Variable without name: ${''}

So variable variables are existing. Meaning that this is working
$a = 'test';
$$a = 'Hello';
echo ${'test'}; //outputs 'Hello'
But now I've come across some rather strange code using a variable without a name:
function test(&$numRows) {
$numRows = 5;
echo ' -- done test';
}
$value = 0;
test($value);
echo ' -- result is '.$value;
test(${''}); //variable without name
http://ideone.com/gTvayV Code fiddle
Output of this is:
-- done test -- result is 5 -- done test
That means, the code is not crashing.
Now my question is: what exactly happens if $numRows value is changed when the parameter is a variable without name? Will the value be written into nirvana? Is that the PHP variable equivalent to /dev/null?
I wasn't able to find anything specific about this.
Thanks in advance
${''} is a valid variable which name happens to be an empty string. If you have never set it before, it is undefined.
var_dump(isset(${''})); // if you have never set it before, it is undefined.
You don't see any error because you disabled the NOTICE error message.
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo ${''}; // Notice: Undefined variable:
You can set it like this:
${''} = 10;
echo ${''}; // shows 10
Now my question is: what exactly happens if $numRows value is changed
when the parameter is a variable without name?
There's no such thing as a variable without name, an empty string in PHP is a totally valid name.
Maybe I'm wrong, but in PHP, all varibles can be accessed by their names (or more precisely, the string representation of their name), and since an empty string is still a string, it counts as a valid name.
Think about variables like an array key-value pair. You can create an array key with an empty string:
$arr = [];
$arr[''] = 'appul';
var_dump($arr['']); // prints: string(5) "appul"
$arr[''] = 'ponka';
var_dump($arr['']); // prints: string(5) "ponka"
Whenever you access $arr[''], you address the same value.
You can access all variables as a string using the $GLOBAL variable too, so you can examine what happens to your "nameless" variable:
${''} = 'ponka';
var_dump($GLOBALS['']); // prints: string(5) "ponka"
${''} = 'appul';
var_dump($GLOBALS['']); // prints: string(5) "appul"
Will the value be written into nirvana? Is that the PHP variable equivalent to /dev/null? I wasn't able to find anything specific about this.
No, it doesn't go to nirvana, it sits quietly in the global space, and it's a little bit trickier to access it, but otherways, it's a normal variable like any others.

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

Add two $row together in one php echo

I'm not even sure if what I am trying to do is possible, I have a simple php echo line as below..
<?php echo $T1R[0]['Site']; ?>
This works well but I want to make the "1" in the $T1R to be fluid, is it possible to do something like ..
<?php echo $T + '$row_ColNumC['ColNaumNo']' + R[0]['Site']; ?>
Where the 1 is replaced with the content of ColNaumNo i.e. the returned result might be..
<?php echo $T32R[0]['Site']; ?>
It is possible in PHP. The concept is called "variable variables".
The idea is simple: you generate the variable name you want to use and store it in another variable:
$name = 'T'.$row_ColNumC['ColNaumNo'].'R';
Pay attention to the string concatenation operator. PHP uses a dot (.) for this, not the plus sign (+).
If the value of $row_ColNumc['ColNaumNo'] is 32 then the value stored in variable $name is 'T32R';
You can then prepend the variable $name with an extra $ to use it as the name of another variable (indirection). The code echo($$name); prints the content of variable $T32R (if any).
If the variable $T32R stores an array then the syntax $$name[0] is ambiguous and the parser needs a hint to interpret it. It is well explained in the documentation page (of the variable variables):
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
You can do like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
$b = $$a;
echo "<pre>";
print_r($b[0]['Site']);
Or more simpler like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
echo "<pre>";
print_r(${$a}[0]['Site']);

PHP variables variable not displaying if passed as an array or object

This works with simple variables. But it shows empty result with complex variables. AM I MISSING SOMETHING HERE? or is there anyother way around. Thanks.
#1. This works with simple variables.
$object = "fruit";
$fruit = "banana";
echo $$object; // <------------ WORKS :outputs "banana".
echo "\n";
echo ${"fruit"}; // <------------ This outputs "banana".
#2. With complex structure it doesn't. am I missing something here?
echo "\n";
$result = array("node"=> (object)array("id"=>10, "home"=>"earth", ), "count"=>10, "and_so_on"=>true, );
#var_dump($result);
$path = "result['node']->id";
echo "\n";
echo $$path; // <---------- This outputs to blank. Should output "10".
Not exactly using variable variables, but if you want to use a variable as the var name, eval should work
$path = "result['node']->id";
eval("echo $".$path.";");
From php.net's page on variable variables
A variable variable takes the value of a variable and treats that as the name of a variable.
The issue is that result['node']->id is not a variable. result is the variable. If you turn on error reporting for PHP notices you will see the following in your output:
PHP Notice: Undefined variable: result['node']->id ...
This can be solved as follows:
$path = "result";
echo "\n";
echo ${$path}['node']->id;
The curly braces around $path are required.
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
If not present the statement is equivalent to
${$path['node']->id}
which will result in the following output:
PHP Warning: Illegal string offset 'node' in /var/www/html/variable.php on line 18
PHP Notice: Undefined variable: r in /var/www/html/variable.php on line 18
PHP Notice: Trying to get property of non-object in /var/www/html/variable.php on line 18

Variable Variable wont work with array

I'm trying to do a variable variable, but it wont work when I use it in the code below.
I keep getting:
Notice: Undefined variable: C in C:\web\apache\htdocs\cats-test.php on line 8
This only wont work when used with an array. Can you help?
$Consumer = array(
"a" => "Apparel",
"b" => "Books & Stationary",
);
$cat = "Consumer";
echo $$cat['a']; //I'm trying to make this $Consumer['a'];
echo ${$cat}['a'];
It's ambiguous whether you mean $$cat ['a'] or $ $cat['a']. Use brackets.
Be aware of operator priorities. ${$cat}['a'] should work better.
When accessing an array key in a variable variable, enclose the variable in {} to be certain that PHP expands the correct set of characters ($cat) as a variable.
echo ${$cat}['a'];
// Apparel

Categories