pass values using header in php - php

1.php
<?php
header( 'Location: 4.php?$x=1&y=2&z=3' );
?>
sends the value of x ,y ,z
4.php
<?php
print '<pre>';
$a= $_GET ;
echo $a[x];
print '</pre>';
?>
when we call 1.php it is redirected to 4.php
it displays the value of x correct but it gives error
Notice: Use of undefined constant x - assumed 'x' in C:\wamp\www\4.php on line 6
why it gives error ?

Common bug again...
echo $a[x];
should be
echo $a['$x'];
in echo $a[x];, x is treated as (so called) "bare string", and PHP will look for a constant named x, which does not exist.
On the other hand, you need to get the $x key in the $_GET superglobal, which is populated by PHP from your URL.

You need this instead:
echo $a['$x']
Note that you're passing in $x in the query string. Make sure you use the appropriate string key of $_GET, or $a in your case.

Related

Php explode and echo second element

I have a file that use's this code
<?php echo $block->getStoreName(); ?>
to output the following on the website
First Second Third
However I only want to only output the Third element of the string above, First Second do not change they always stay the same.
Third
I'm using this code to retrieve the Third part of the string
echo explode('First Second', $block->getStoreName())[1];
Its throwing up an error.
Error filtering template: Notice: Undefined offset: 2 in
/home/xyz/m230.xyz.com/app/code/Vendor/Siteinfo/view/frontend/templates/storename.phtml
on line 1
Line 1 in storename.phtml is
<?php echo explode('First Second', $block->getStoreName())[1]; ?>
I'm unsure if thats the correct way of doing it.
UPDATE - Have attempted a clearer explanation of what I'm trying to achieve.
echo explode(' ', $block->getStoreName())[2];
This should do the trick.
// checks if string has "Unique"
if(mb_strpos($block->getStoreName(),'Unique') !== false){
// prints "Unique"
echo "Unique";
}

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.

Why the 'NULL' value of a variable is not displayed using echo and print but displayed with var_dump() function?

As we all know if a variable is created without a value, it is automatically assigned a value of NULL.
I have following code snippets :
<?php
$name;
echo $name;
?>
AND
<?php
$name;
print $name;
?>
Both of the above code snippets' output is as below(it's exactly the same) :
Notice: Undefined variable: name in C:\xampp\htdocs\php_playground\demo.php on line 7
I have another code snippet :
<?php
$name;
var_dump($name);
?>
The output of above(last) code snippet is as below :
Notice: Undefined variable: name in C:\xampp\htdocs\php_playground\demo.php on line 8
NULL
So, my question is why the value "NULL" is not getting displayed when I tried to show it using echo and print?
However, the "NULL" value gets displayed when I tried to show it using var_dump() function.
Why this is happening?
What's behind this behavior?
Thank You.
The problem you're having is that NULL isn't anything - it's the absence of a value.
When you try to echo or print it, you get the notice about an Undefined Variable because the value of $name is not set to anything, and you can't echo the absence of something.
$name;
var_dump($name);
The output of this will be NULL to tell you that the variable had no value. It's not a string with the value of "NULL", it's just NULL, nothing, the absence of something.
Compare this to the following:
$name = '';
var_dump($name);
This outputs string(0)"" - this is telling you that $name DID have a value, which was a string which contained no characters ("") totalling a length of 0.
Finally, look at the following:
$name = 'test';
var_dump($name);
This outputs string(4)"test" - a string containing test, which had a length of 4

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

Categories