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

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!

Related

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 - Variable Safe Output [duplicate]

This question already has answers here:
Is there a better PHP way for getting default value by key from array (dictionary)?
(8 answers)
Closed 3 years ago.
I'm fetching information from a unofficial API. This API is very large and sometimes doesn't have all elements in it. I'm trying to display values from this API on my site without any errors.
What I've done is check the JSON values like so, to prevent errors:
echo (isset($json['item'])) ? $json['item'] : '';
Works, but it looks very unorganized. I've thought about creating a function to handle safe output, like so:
public function safeoutput($input, $fallback = '') {
if(isset($input)) {
return $input;
}
if(empty($input) || !isset($input)) {
return $fallback;
}
}
and then doing:
echo $engine->safeoutput($json['item'], 'Unavailable');
That unfortuanlly still outputs the Undefined variable error.
I was wondering if there's a better way to handle such information like I showed in the example.
Problem is that the key might not be set, so you would have to check it:
public function safeoutput($input, $key, $fallback = '') {
if(isset($input[$key])) {
return $input;
}
if(empty($input[$key]) || !isset($input[$key])) {
return $fallback;
}
}
Or you can have a shorter version:
public function safeoutput($input, $key, $fallback = '') {
if(array_key_exists($key, $input) && !empty($input[$key])){
return $input[$key];
}
return $fallback;
}
And call the method with the array and the key:
echo $engine->safeoutput($json, 'item', 'Unavailable');

PHP return syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the PHP ? : operator called and what does it do?
Can someone please tell me what this 'return' php code means / does:
return ($status=='SUCCESS' && $blocked=='YES') ? $reason : false;
I'm familiar with the regular return $variable type of statements in php, but I don't get what the specific brackets ( ) and ? question marks and the ": false" does.
(this is the return statement at the end of a php function)
It's a ternary statement. It's basically a shorthand notation for if/else.
In your example it would read like: If $status is equal to "success" and $blocked is equal to "Yes" return $reason, else, return false;
That's a ternary, or conditional operator, it's the same as if you had:
if($status=='SUCCESS' && $blocked=='YES'){
return $reason;}
else{
return false;
}
It means the same as this:
if($status == 'SUCCESS' && $blocked == 'YES')
{
return $reason;
}
else
{
return false;
}

PHP to compare and return a variable in a querystring

I need a bit of simple PHP code that can return a specified variable if any one of three variables is contained within a query string. Probably easier to explain like this:
if {querystring} contains {var1} or {var2} or {var3} return {var1}
This is expands on the following question: Creating a canonical with PHP
I need to add said code to one of the variables specified in function params, in the linked question.
function evaluateThis($var1,$var2,$var3) {
if((strpos($string,$var1) !== false) || (strpos($string,$var1) !== false) || (strpos($string,$var1) !== false)) {
return $var1;
}
else { return 'string not found'; }
}
Is this what you mean
If you want to analyse the query string of the current request:
array_search($var1,$_GET)!==false OR array_search($var2,$_GET)!==false ....
else:
$vars = array();
parse_string($queryString,$vars);
if(array_search($var1,$vars)!==false OR array_search($var2,$vars)!==false ...
.

original variable name passed to function? [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();

Categories