Work with variables in two conditions - php

I have a problem with the work with variables from one condition in the second condition. I have something like that:
<form name="exampleForm" method="post">
...
<input type="submit" name="firstSubmit" value="Send">
<input type="submit" name="secondSubmit" value="Show">
</form>
<?php
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
if(isset($_POST['secondSubmit'])) {
echo $functionOutput;
}
?>
When I need to work with variable $functionOutput from first condition I always get an error message (undefined variable). How I can solve this problem?

I'm not sure what you are trying to do exactly, but when you press your second button, the variable $functionOutput is not defined as the first condition is false so that whole section is skipped.
Note that variables are lost as soon as the script ends. You could look into sessions and use session variables to solve that, but it depends a bit on what you want to do exactly.
To use sessions, you would have to move your whole php block to before where you start outputting html and do something like:
<?php
session_start();
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
return $c;
}
$_SESSION['output'] = a();
}
// start html output
?>
<doctype .....
<html ....
// and where you want to echo
if(isset($_POST['firstSubmit'])) {
echo "The result is {$_SESSION['output']}";
}
if(isset($_POST['secondSubmit'])) {
echo $_SESSION['output'];
}

<?php
$functionOutput = "";
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
if(isset($_POST['secondSubmit'])) {
echo $functionOutput;
}
?>
Should fix it. It's happening because you're declaring $functionOutput inside your first IF statement.

As $functionOutput is not initialized when you are calling if(isset($_POST['secondSubmit']))
<?php
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
$functionOutput='12';//intialize
if(isset($_POST['secondSubmit'])) {
echo $functionOutput;
}
?>
**OR**
<?php
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
if(isset($_POST['secondSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a - $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
echo $functionOutput;
}
?>

Related

PHP function doesn't see a global variable if it's not declared global outside of the function

The code from the official example:
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
Result: 2
If $a and $b are declared global outside the function then it works.
<?php
global $a, $b; // ← added this
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
Result: 3
PHP 5.6.4
Why so?
UPDATE: Thanks for the comments. Apparently, the problem is in the CMS. I will look for the reason in it.

Reset global variable in php after function return

$a = 0;
function a(){
global $a;
$success = b();
if ($success) {
return $a;
$a = 0;
}
}
function b(){
global $a;
$a += 9;
return true;
}
In function a if function b is success then i want to return $a and then set $a to 0
how can i do this ?
You could try creating a temporary variable and then set $a before returning.
$temp = $a;
$a = 0;
Return $temp;

php global variable in functions

this is a very simple program, but when i run it, it just out put the first echo statement, which is 3, others are not displaying. i declared it global in the function, why it dose not work, strangely, when i was coding another program declaring a global array variable in a function it works perfectly, please explain it in detail, thanks
$a = 1;
$b = 2;
$c = $a + $b;
echo $c ."<br>";
function aaa()
{
global $a;
global $b;
$d = $a + $b;
echo $a ."<br>";
function ccc()
{
global $d;
$e = $c + 1;
echo $e;
}
}
The problem with your code is that you've put the function ccc inside the funciton aaa and that is not the correct way of doing what you're trying to do.
The correct way would be to create a Class aaa and then declaring ccc as its method.
$a = 1;
$b = 2;
$c = $a + $b;
$d = 0;
function aaa() {
global $a;
global $b;
global $d;
$d = $a + $b;
echo $a ."<br>";
}
function ccc() {
global $c;
global $d;
$e = $c + 1;
echo $e;
}
echo $c ."<br>";
aaa();
ccc();

if a variable is referenced inside a function then what happens

function a(&$c, &$d){
$c = &$d;
}
$a = 1;
$b = 2;
a($a, $b);
echo $a;
output is 1,but shouldn't it be outputting 2 as $c is referencing $d. $c and $a reference to the same value,then $c refer to the value of $d which refer to $b so ultimately $a should refer to $b, isn't it correct?
For the operation you seek you must remove the ampersand reference used within the function a.
Example
function swap (&$one, &$two) {
$tmp = $one; // One is stored temporarily
$one = $two; // Two is stored in One
$two = $tmp; // Temporary data retrieved and stored in two
unset($tmp); // Temporary variable destroyed
}
// Set the variables
$a = 1;
$b = 2;
echo $a . " - " . $b . "<br />"; // See output as: 1 - 2
swap($a, $b);
echo $a . " - " . $b; // See output as: 2 - 1
in line
$c = &$d;
$a is referring to the address of $b but not to $b, then you can try this:
<?php
function foo(&$c, &$d){
$c = $d;
}
$a = 1;
$b = 2;
foo($a, $b);
echo $a;
?>

Conflicting static vars with non-static ones in changing values

Consider the following code:
class A {
private $a;
function f() {
A::a = 0; //error
$this->a = 0; //error
$self::a = 0; //error
}
}
How can I change $a in f()?
You were close. Either:
self::$a = 0; //or
A::$a = 0;
If it's static, or:
$this->a = 0;
If it's not.
I believe the syntax is:
self::$a
obviously we all have been tricked by the title of the question, though here is how you change $a's value.
<?php
class A {
private $a;
function f() {
//A::a = 0; //error
$this->a = 10; //ok
//$self::a = 0; //error
}
function display() {
echo 'a : ' . $this->a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
?>
Output:
a : 10
if you want $a to be static use the following:
<?php
class A {
private static $a;
function f() {
//A::$a = 0; //ok
//$this->a = 10; //Strict Standards warning: conflicting the $a with the static $a property
self::$a = 0; //ok
}
function display() {
echo 'a : ' . self::$a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
// notice that you can't use A::$a = 10; here since $a is private
?>

Categories