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();
Related
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!
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();
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 5 years ago.
I wrote a function that I thought cannot be more basic:
In functions.php:
function testowa() {
$stringToReturn = "pies";
return $stringToReturn;
}
Then I'm trying to call it in single.php:
include_once ('functions.php');
testowa();
var_dump($stringToReturn);
And var_dump displays NULL.
Where could I possibly do anything wrong?
You have to assign the function's response to a variable. Try
$stringToReturn = testowa();
var_dump($stringToReturn);
#MichaĆ Skrzypek update your function.php file like below:
<?php
function testowa() {
global $stringToReturn;
$stringToReturn = "pies";
return $stringToReturn;
}
Some ways to rome.
Return Value Version:
function testowa() {
return = "pies";
}
print testowa();
Reference Version
function testowa(&$refer) {
$refer = "pies";
}
$refer = '';
testowa($refer);
print $refer;
Global Version
function testowa() {
global $global;
$global = "pies";
}
$global='';
testowa();
print $global;
But take the Return Value Version and avoid Global Version
I get a lot of array from some API and I need to check weither some variable exist or not.
I have a lot of block that look like that :
if (isset($var))
$varToSet = $var;
else
$varToSet = '';
So I've decided to make a function for that. I came with that:
function setVar($var)
{
if (isset($var))
return $var;
return '';
}
But as I would expected I got the error Undefined variable, I figured out I needed to passe the argument by reference so I would get the following prototype :
function setVar(&$var);
And It was working perfectly until now, here's an example of my problem :
// works fine
$var = "test";
$varToSet = setVar($var);
// works fine
$var = "test";
$varToSet = setVar($doesNotExist);
// works fine
$var = "test";
$varToSet = setVar($doesNotExist['index']);
// doesn't work
$var = "test";
$varToSet = setVar($var['index']);
In the last example I get Illegal string offset 'index and Only variables can be passed by reference PHP errors.
I know why I got those errors, I just can't figure out how overcome this problem.
i mainly use property_exists to check if a value exist on a json object.
function getFromJson($json,$value)
{
if (property_exists(json, $value)) {
return $json->$value;
}
return null;
}
function get($var,$value = null)
{
if (is_null($value)) {
return $var;
}
if (is_object($var) && property_exists($var, $value)) {
return $json->$value;
}
if (is_array($var)) {
return $var[$value];
}
return $var;
}
The error gives you the answer. Your variable is a string. But you are trying to access an array element by using brackets [ ].
And the second is caused by invalid refference.
This is passing by reference:
$variable = 'test';
myFunction($variable);
and this is passing by value:
myFunction('test');
That's a big difference!
You can't call string as array
$varToSet = setVar($var['index']);
You can change the line to:
echo $var['index'];
and you will still have the same error/warning.
If you want to validate if array variable is set use
isset($var['index'])
but it returns value, not a refference
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();