PHP echo session value to a new session? - php

Is it possible to achieve something like this?
$_SESSION["new"] = "This are the values of $_SESSION['A'] & $_SESSION['B']";
I want to echo the "new" session value on a different page for my user to check.
I tried various things such as the code below but it doesn't work.
$_SESSION["new"] = <?php echo $_SESSION['A']?>;

Yes, you can assign multiple session variables by using the concatenation operator.
$_SESSION["new"] = "This are the values of ".$_SESSION['A']."&".$_SESSION['B'];
On the page, you want to display, simply print the session variable.
echo $_SESSION["new"];
Please make sure,on both pages, you are using session_start() function

$_SESSION["new"] = 'foo bar';
You can simply assign any value to your session variables. you can read more here
if you are sure of the type of value in $_SESSION["A"] and $_SESSION["B"], and assuming in your case they are both string, you can do the following:
$_SESSION["new"] = sprintf('This are the values of %s & %s', $_SESSION['A'], $_SESSION['B']);

Related

what is session_reset() exactly?

I want to know what is session_reset() for and when should we use it ?
when I use it I get an error like this: "Call to undefined index session_reset()".
I hope you know something about it .
Thank you in advance.
session_reset — Re-initialize session array with original values.
Example
first create a session variable
<?php
session_start();
$_SESSION["A"] = "Some Value";
echo $_SESSION["A"];
//Output: Some Value
//if you need to rollback the session values after seting new value to session variables use session_reset()
$_SESSION["A"] = "Some New Value"; // set new value
session_reset(); // old session value restored
echo $_SESSION["A"];
//Output: Some Value
?>

How to call double echo or double variable in php

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

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']);

Session variables turning into arrays when I put them into variables

I have a session for login data, and I am trying to take it and put it into a variable to shorten whenever I want to echo or use the ID, Username, or Email. What is happening is that now I have it in a variable, but when I echo it, it shows it in an array.
Here is my variable data.
$User = htmlentities(ucwords($_SESSION['user']['username']));
$Email = htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8');
If I echo any of these, it returns "Array".
EDIT
I fixed my solution by changing how I get my $_SESSION data, and using unset to get rid of the array. Now I can just do this,
$User = $_SESSION['user']["username"];
$Email = $_SESSION['user']["email"];
Thank you for reminding me about how I'm even getting my session data in the first place =]
If you are using classes, where you specify all your variables, maybe try this:
$User = $_SESSION['user']->username;
echo $User;
where user is your class and username is your variable
WHen you add it to your session, you need to do soething like this:
$_SESSION['user'] = new user();
$_SESSION['user']->username= 'whatever value it is';

Using array value with index as Variable Variable

The title may be a little confusing. This is my problem:
I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:
$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;
That would print: "hello". Now, I've got a problem with this:
$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;
That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?
Thanks in advance.
For the moment, I could only think of something like this:
<?php
// literal are simple
$literal = "Hello";
$vv = "literal";
echo $$vv . "\n";
// prints "Hello"
// for containers it's not so simple anymore
$container = array("Hello" => "World");
$vv = "container";
$reniatnoc = $$vv;
echo $reniatnoc["Hello"] . "\n";
// prints "World"
?>
The problem here is that (quoting from php: access array value on the fly):
the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.
Would PHP allow the subscript notation anywhere, one could write this more dense as
echo $$vv["Hello"]
Side note: I guess using variable variables isn't that sane to use in production.
How about this? (NOTE: variable variables are as bad as goto)
$variablename = 'array';
$key = 'index';
echo $$variablename[$key];

Categories