In PHP: How to Call a $Variable Inside One Function That Was Defined Previously Inside Another Function

In PHP: How to call a $variable inside one function that was defined previously inside another function?

you should create the var in the class, not in the function, because when the function end the variable will be unset (due to function termination)...

class helloWorld {

private $var;

function sayHello() {
echo "Hello";
$this->var = "World";
}

function sayWorld() {
echo $this->var;
}

}
?>

If you declare the Variable as public, it's accessible directly by all the others classes, whereas if you declare the variable as private, it's accessible only in the same class..

<?php
Class First {
private $a;
public $b;

public function create(){
$this->a=1; //no problem
$thia->b=2; //no problem
}

public function geta(){
return $this->a;
}
private function getb(){
return $this->b;
}
}

Class Second{

function test(){
$a=new First; //create object $a that is a First Class.
$a->create(); // call the public function create..
echo $a->b; //ok in the class the var is public and it's accessible by everywhere
echo $a->a; //problem in hte class the var is private
echo $a->geta(); //ok the A value from class is get through the public function, the value $a in the class is not dicrectly accessible
echo $a->getb(); //error the getb function is private and it's accessible only from inside the class
}
}
?>

call function variable inside function variable

The general trick here is to use () the Closure holding variable by reference:

$function = function($parameter) use ($variable, &$function) {
//Do some stuff
$function($something);
}

How to call a function from a string stored in a variable?

$functionName() or call_user_func($functionName)

Pass PHP variable created in one function to another

You could make the variable global:

function add_custom_price( $cart_object ) {
global $newVar;
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar = $value['data']->price;
}
}

function function_name() {
global $newVar;
echo $newVar;
}

Or, if $newVar is already available in the global scope you could do:

function function_name($newVar) {
echo $newVar;
}

// Add the hook
add_action( 'another_area', 'function_name' );

// Trigger the hook with the $newVar;
do_action('another_area', $newVar);

How to use variable in another function php

You have to set the variable inside Pokemon 1 to global, after you do that, make sure you call function Pokemon 1 first, else the variable inside pokemon 2 with the variable from pokemon 1 will be undefined.

Also, why don't you just declare the variable outside the scope of a function?

 $path = "";

function pokemon1(){
....
global $path...;
}

function pokemon2(){
....
global $path...;
}

PHP function use variable from outside

Add second parameter

You need to pass additional parameter to your function:

function parts($site_url, $part) { 
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
}

In case of closures

If you'd rather use closures then you can import variable to the current scope (the use keyword):

$parts = function($part) use ($site_url) { 
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
};

global - a bad practice

This post is frequently read, so something needs to be clarified about global. Using it is considered a bad practice (refer to this and this).

For the completeness sake here is the solution using global:

function parts($part) { 
global $site_url;
$structure = 'http://' . $site_url . 'content/';
echo($structure . $part . '.php');
}

It works because you have to tell interpreter that you want to use a global variable, now it thinks it's a local variable (within your function).

Suggested reading:

  • Variable scope in PHP
  • Anonymous functions

How to extend a method in php

class myClassName{
public $a, $b, $c;

function Init_funtionABC(){
$this->a = '123';
$this->b = '456';
$this->c = '789';
}

function funtionOne(){

Init_funtionABC();

//you can access $this->a , $this->b , $this->c here

$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}

function funtionOne(){

Init_funtionABC();

//you can access $this->a , $this->b , $this->c here

$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}

}


Related Topics



Leave a reply



Submit