Access variable from outside function (PHP) [duplicate] - php

This question already has answers here:
PHP: how to access variables inside a function that have been declared outside of it?
(3 answers)
Closed 1 year ago.
I am trying to access this variable into function but its give me an error Undefined variable: ERROR.
<?php
$ERROR["emptyEmail"] = "empyt email";
$ERROR["emptyPassword"] = "empty password";
function validateLogin($data) {
if (empty($data["email"])) {
return $ERROR["emptyEmail"];
} else if (empty($data["password"])) {
return $ERROR["emptyPassword"];
} else {
return "valid";
}
}
?>

Insert this inside the function:
global $ERROR;
So, the variable can be accessed inside the function scope (see global keyword).
function validateLogin($data) {
global $ERROR;
...
}
Alternatively you can access to all variables that are outside the function using $GLOBALS:
$GLOBALS['ERROR']

Related

Call a function inside a class method and use its variables [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I'm new to PHP, i made some research but couldn't find a clear answer.
In a functions.php file i have a function that contains some variables that i need, and a classes.php file containing a class with a method that needs the values from the variables of the function in functions.php.
The Question is: why the echoed variable is outputted but the other variables are called as Undefined.And how can i call and use them.
Example:
functions/functions.php
<?php
// functions.php file
function incFunc(){
$inc_var1 = "Marco";
echo $inc_var1; //this variable output Marco without problem
echo "<br>";
echo "<br>";
$inc_var2 = "Polo"; //this is the variable i want to use in the class method.
}
?>
classes/classes.php
<?php
// classes.php file
include '../functions/functions.php';
class theClass
{
public function classFunc(){
incFunc();
echo $inc_var2; //Notice: Undefined variable: inc_var2
}
}
$obj = new theClass();
$obj->classFunc();
?>
Outputs:
Marco
Notice: Undefined variable: inc_var2 in...
If you want to use variables from incFunc function scope in the class you have to return the value
Example:
function incFunc($returnVar){
$inc_var1 = "Marco";
$inc_var2 = "Polo";
if ($returnVar == 'var1') {
return $inc_var1;
} else {
return $inc_var2;
}
}
Than,in your class method, save the result of a function in an variable:
public function classFunc(){
$inc_var1 = incFunc('var1');
// do stuff
}
This is just a basic example, if you want to include multiple variables at once, you can return an array instead of single variables.

How to use a variable set inside function [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
Hi I am new to php and I just run into a problem. I would like to get local variable data outside the function after it was called.
function MyFunction() {
$x = set
}
if($x == "set"){
code...
}
You can't do that. Instead return the value from the function.
function MyFunction() {
$x = "set";
return $x
}
if(MyFunction() == "set"){
code...
}

Unable to access included variable from separate PHP script [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I'm not new to programming but I am very new to PHP. I just can't figure out why this variable is not being recognised. I have a file called utils.php in directory utils like this:
<?php
$the_var = 'A'
function foo($bar) {
echo $bar;
}
?>
...and another file called work.php in a parent directory of utils like this:
<?php
include('utils/utils.php');
function doIt() {
echo $the_var; // is always empty
foo('bar'); // no problem
}
?>
Why can't the variable $the_var be accessed?
Variable inside function is not global. If you can access to variable $the_var use
function doIt($the_var) {
echo $the_var;
foo('bar'); // no problem
}
or
function doIt() {
echo $GLOBALS['the_var'];
foo('bar'); // no problem
}

PHP functions and variables [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
index.php:
<?php
require("lib.php");
echo getName();
?>
lib.php:
<?php
$name = "Matej";
function getName() {
return $name;
}
?>
Code doesnt work, i think its becouse PHP cant get variable form outsite of function. how to fix it?
You can lean about variable scope here. If you want using global variables, then use global keyword.
function getName() {
global $name;
return $name;
}
But... well, global is evil. In your case, a function like this is useless. Simply use $name.
You're trying to get value of $name outside function, you could call the function sending the value to it and it should work.
The reason is because the variable does not have the global keyword!
$name = 'hello';
function name() {
return global $name;
}

class scopes in PHP [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
This is an outline of a problem which I'm struggling to solve in my code. I guess my knowledge of scope isn't that great.. I don't understand why the function getGreeting is giving a parse error.
<?php
class Class_1 {
public $t;
public function __construct() {
$this->t = "hello world";
}
public function helloWorld() {
return $this->t;
}
}
$x = new Class_1();
function getGreeting() {
return $x->helloWorld();;
}
echo getGreeting();
?>
the error I get is:
Fatal error: Call to a member function helloWorld() on a non-object.
Because you need to initialize object in the function to access it's methods from it :
function getGreeting() {
$x = new Class_1();
return $x->helloWorld();;
}
Example

Categories