PHP global variables not working inside a function [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php function variable scope
I am using below code to test with a global variable. It seems that a global variable cannot be compared inside a function.
Why is it not displaying 'hello world' in the output?
Below is the code that I am trying:
<?php
$bool = 1;
function boo() {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
When I remove function boo(), 'hello world' is displayed. Why is it not displaying when function exists?

use global $var to access your variable
<?php
$bool = 1;
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
boo();
?>
Or a safer way using pointers would be to
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}

Looks like homework, still:
<?php
$bool = 1;
boo();
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
Or
<?php
$bool = 1;
boo(&$bool);
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>

Call you function, and pass $bool as a parameter and return the value.
$bool = 1;
$bool = boo($bool);
function boo($bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
return $bool;
}

use this way
$bool = 1;
function boo($bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
boo($bool);

Related

If statement inside setted function

How to edit already setted function inside () if I want inside that function if statement
<?php
$a = 1;
function writeMsg($x) {
echo $x;
}
writeMsg(
Hello,
if ($a == 1) {
echo "men";
} else {
echo "women";
}
);
?>
I think you want to prepare the message first then write message.
<?php
$a = 1;
function writeMsg($x) {
echo $x;
}
$message = 'Hello, '. ( $a == 1 ? 'men' : 'women');
writeMsg($message);

Is there an isdate PHP function?

I saw an inbuilt function called isdate in the user function below and when I checked the PHP documentation I didn't find it. Does it exist and what does it do; or is this simply a typo?
function mystery($a, $b, $c) {
$result = null;
if (strlen(trim($a)) == 0) {
$result = $c;
}
else {
if (strtolower(trim($b)) == "n") {
if (!is_numeric($a)) {
$result = $c;
}
else {
$result = trim($a);
}
}
else {
if (strtolower(trim($b)) == "d") {
if (!isdate($a)) {
$result = $c;
}
else {
$result = trim($a);
}
}
else {
$result = $a;
}
}
}
return($result);
}
Perhaps, this isn't really an answer since I don't know what the original function is for and to be honest I don't care. Whatever, you can do several simplifications to make it more readable, to avoid duplicate processing, the useless $result variable and in particular to remove these unreadable nested if/else, in other words to remove all this useless noise:
function mystery($a, $b, $c) {
$trima = trim($a);
if ( empty($trima) )
return $c;
$b = strtolower(trim($b));
if ( $b == "n" )
return is_numeric($trima) ? $trima : $c ;
if ( $b == "d" )
return isdate($trima) ? $trima : $c ;
return $a;
}
I hope this will help you to understand what this function is supposed to do (perhaps a context will be helpful).

Combining two reference variables

Is it somehow possible to "glue" two reference variables?
For example
$more = &$first.':'.&$second;
Using this, i receive a syntax error, an unexpected &.
Full code
$numOf = isset($_GET['numof']) ? $_GET['numof'] : 'x';
if($numOf == 1) {
$more = &$first;
} else if($numOf == 2) {
$more = &$first.':'.&$second;
} else {
$more = '';
}
$results = array(); // array with results from database
foreach($results as $res) {
$first = $res[0];
$second = $res[1];
echo $more.$res[3];
}
You should use a Closure to achieve what you want. Indeed, you need PHP 7(Maybe 5.6, can't tell as I can't test) in order to achieve desired result. Here's an example:
<?php
$first = "a";
$second = "b";
$more = function(){ global $first,$second; return $first.$second; };
echo $more()."<br>"; // This will output ab
$first = "b";
echo $more(); // This will output bb
One thing you can do is the following :
$ar = array(&$first, &$second);
$more = implode(":", $ar);
not directly, at least not that i know of.
what you could do is create a class with a method that automatically combines the values. if you only want string output, you can use the magic method __tostring, so you can use the class directly:
class combiner
{
private $a;
private $b;
public function __construct(&$a, &$b)
{
$this->a = &$a;
$this->b = &$b;
}
public function __tostring() {
return $this->a.":".$this->b;
}
}
$ta = "A";
$tb = "B";
$combined = new combiner($ta, $tb);
echo $combined; //A:B
$ta = "C";
echo $combined; //C:B
You can get your required result by:
<?php
function more($first, $second){
if(!empty($_GET['numof'])){
if($_GET['numof']==1)
return $first;
elseif($_GET['numof']==2)
return $first.':'.$second
}
return '';
}
$results = array(); // array with results from database
foreach($results as $res) {
$first = $res[0];
$second = $res[1];
echo more($first, $second).$res[3];
}

How to override a variable in PHP that's in two functions

function d20() {
$rollAll = false;
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20();
}
rollAll();
I want to make it so that whenever I call the rollAll function, $rollAll will be true and it won't echo the roll. Sorry if this problem seems really stupid, but I'm new to PHP.
Thanks.
#Rizier123 Answered just i pass the variable from rollAll to d20
function d20($rollAll) {
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20($rollAll );
}
rollAll();
set the variable $rollAll in global scope or pass as function params
function d20($rollAll) {
$rollAll = false;
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20( $rollAll );
}
rollAll();
or
$rollAll = true;// or false
function d20() {
$rollAll = false;
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20( );
}
rollAll();

Put IF condition inside a variable

Is there any way to put conditions within a variable and then use that variable in an if statement? See the example below:
$value1 = 10;
$value2 = 10;
$value_condition = '($value1 == $value2)';
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
I understand this may be a bizarre question. I am learning the basics of PHP.
No need to use strings. Use it directly this way:
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
Or to evaluate, you can use this way using ", as it expands and evaluates variables inside { ... }.
I reckon it might work! Also, using eval() is evil! So make sure you use it in right place, where you are sure that there cannot be any other input to the eval() function!
Depending on what you are trying to do, an anonymous function could help here.
$value1 = 10;
$value2 = 10;
$equals = function($a, $b) {
return $a == $b;
};
if ($equals($value1, $value2)) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
However, I would only do it like this (and not with a regular function), when you make use of use ().
== operator evaluates as a boolean so you can do
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
Just assign result of comparision to variable.
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
An if statement tests a boolean value. You could have something like this:
if (true) {
You can assign boolean values to a variable:
$boolValue = true;
You can use variables in your if statement:
if ($boolValue) {
// true
In your example:
$value_condition = $value1 == $value2; // $value_condition is now true or false
if ($value_condition) {

Categories