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();
Related
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.
$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;
I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;
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;
?>
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;
}
?>