original variable name passed to function? [duplicate] - php

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
PHP: Get name of variable passed as argument
How to get a variable name as a string in PHP?
If I have
$abc = '123';
function write($var){
}
How would I inside write find out that the variable now represented by $var was called $abc?

It's not possible. Even pass-by-reference won't help you. You'll have to pass the name as a second argument.
But what you have asked is most assuredly not a good solution to your problem.

You can not get the variable name, but if you want it for debugging, then you can use PHP's built-in debug_backtrace(), and I recommend to take a look on Xdebug as well.
With this function, you can get some data on the caller, including the file and line number, so you can look up that line manualy after running the script:
function debug_caller_data()
{
$backtrace = debug_backtrace();
if (count($backtrace) > 2)
return $backtrace[count($backtrace)-2];
elseif (count($backtrace) > 1)
return $backtrace[count($backtrace)-1];
else
return false;
}
Full example:
<?php
function debug_caller_data()
{
$backtrace = debug_backtrace();
if (count($backtrace) > 2)
return $backtrace[count($backtrace)-2];
elseif (count($backtrace) > 1)
return $backtrace[count($backtrace)-1];
else
return false;
}
function write($var)
{
var_dump(debug_caller_data());
}
function caller_function()
{
$abc = '123';
write($abc);
}
$abc = '123';
write($abc);
caller_function();

Related

Fix Function Undefined Variable Not Working [duplicate]

This question already has answers here:
How do I pass undefined vars to functions without E_NOTICE errors?
(4 answers)
Closed 3 years ago.
So i have a function that I am running all variables through so that it returns "" if the variable is not set. It looks like this:
function ck($str){
if(!isset($str) || empty($str)){
$str = "";
} else {
$str = $str;
}
return $str;
}
I have a call on my page:
echo ck($var);
No where on my page is $var defined. This should return a blank string and avoid throwing an error - right???
instead, i am still getting undefined variable error. Can some please explain to me why my function isn't working. Thanks.
$var is not defined. If you want the function to return an empty string you will need to set the parameter to a default value...
function ck($str = ''){
if(!isset($str) || empty($str)){
$str = "";
} else {
$str = $str;
}
return $str;
}
echo ck();

Check multiple variables at once in PHP using IF [duplicate]

This question already has answers here:
Using if(!empty) with multiple variables not in an array
(15 answers)
Closed 3 years ago.
With these variables:
$var1='a';
$var2='b';
How can I check if they are both empty at once without the need of doing this:
if(empty($var1) && empty($var2)){
}
or
if($var1=='' && $var==''){
}
I mean something like:
if(($var1 && $var)==''){
}
Depending on your scale requirements and how large the data you'll be storing in these vars is, you could just do a straight concatenation:
if($var1 . $var2 == '') {
// blank strings in both
}
Alternatively, using empty():
if(empty($var1 . $var2)) {
// blank strings in both
}
Repl.it
How about a utility function:
function is_any_empty() {
$args = func_get_args();
foreach($args as $arg) {
if(empty($arg)) return true;
}
return false;
}
var_dump(is_any_empty("")); // bool(true)
var_dump(is_any_empty("ds", "")); // bool(true)
var_dump(is_any_empty("ds", "dd")); // bool(false)
I mean, that's kinda how you do it. But if you want to ensure that they both are not empty, you'd probably want your if statement to look more like this:
if(!empty($var1) && !empty($var2)) {
// Do stuff! They both exist!
}
I'd prefer an empty check because if $var1 and $var2 haven't been defined, you'll get notices all over your error logs.
Hope that helps!

How do I extract the parameters in a function call from source code? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
PHP: Get name of variable passed as argument
How to get a variable name as a string in PHP?
If I have
$abc = '123';
function write($var){
}
How would I inside write find out that the variable now represented by $var was called $abc?
It's not possible. Even pass-by-reference won't help you. You'll have to pass the name as a second argument.
But what you have asked is most assuredly not a good solution to your problem.
You can not get the variable name, but if you want it for debugging, then you can use PHP's built-in debug_backtrace(), and I recommend to take a look on Xdebug as well.
With this function, you can get some data on the caller, including the file and line number, so you can look up that line manualy after running the script:
function debug_caller_data()
{
$backtrace = debug_backtrace();
if (count($backtrace) > 2)
return $backtrace[count($backtrace)-2];
elseif (count($backtrace) > 1)
return $backtrace[count($backtrace)-1];
else
return false;
}
Full example:
<?php
function debug_caller_data()
{
$backtrace = debug_backtrace();
if (count($backtrace) > 2)
return $backtrace[count($backtrace)-2];
elseif (count($backtrace) > 1)
return $backtrace[count($backtrace)-1];
else
return false;
}
function write($var)
{
var_dump(debug_caller_data());
}
function caller_function()
{
$abc = '123';
write($abc);
}
$abc = '123';
write($abc);
caller_function();

PHP access variable from anonymous function [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Render a variable during creation of anonymous PHP function
I am still quite new with PHP and this bothers me:
class Controller {
...
...
function _activateCar() {
$car_id = $this->data['car']->getId();
// $car_id == 1
$active_car = array_filter($this->data['cars'], function($car){
// $car_id undefined
return $car->getId() == $car_id;
});
}
...
...
}
Why can't the function inside array_filter access the $car_id variable? Keeps saying undefined.
Is there an other way to make $car_id accessible than to make a $_GET['car_id'] = $car_id;? Using the global keyword didn't help.
You need to add use($car_id) to your anonymous function, like so:
$active_car = array_filter($this->data['cars'], function($car) use($car_id){
// $car_id undefined
return $car->getId() == $car_id;
});
Anonymous functions can import select variables with the use keyword:
$active_car = array_fiter($this->data['cars'],function($car) use ($car_id) {
return $car->getId() == $car_id;
});

Automate variable declaration PHP

I want to try and write a function to automate some of the legwork in checking/declaring a variable i.e.
function checkVariable($var)
{
if(!isset($var)||empty($var))
{
return '';
}
else
{
return $var;
}
}
$myvar = checkVariable($myvar);
obviously, this isn't going to work, because the variable doesn't exist prior to declaration and throws an error when you use it as an argument - sooooo, is there a way of doing this?
Pass the variable by reference:
function checkVariable(&$var) {
// …
}
I tend to use
$myvar = (isset($myvar) && !empty($myvar)) ? $myvar : '';
But if you have to do this a lot, and you want to use a function, Gumbo's suggestion is right.

Categories