is it possible to simplify isset? - php

I have a lot of functions with parameter that can be either bool or null. If we simplify my functions I have somethings like this:
funtion ($param) {
if ($param) {
//true
} else {
//false
}
}
However, when I call function(null); it obviously goes into else part of condition. So I have to do
funtion ($param) {
if (isset($param)) {
if ($param) {
//true
} else {
//false
}
}
}
for every similar if condition, which is sort of annoying.
So my question is this:
Is there a possibility to do this type of condition with this type of parameter faster and/or without additional function?

To only validate on true and false, use strict type comparison (===):
function check($param)
{
if ($param === true) {
// It's true. :)
} else if ($param === false) {
// It's false.. :o
}
}
This will ignore it if it is null.

Please read the bottom code for the most useful (but least explanative) function.
The below function solves your dilemma. If $param is true or false then the return gives back the boolean version of the $param (useful for such instances as if $param is a sting or an integer etc).
else, nothing is returned by the function which is defined as a NULL value.
example with type clarity texts:
<?php
//Enter your code here, enjoy!
$test[] = NULL;
$test[] = true;
$test[] = false;
$test[] = "string";
$test[] = 45;
function tester($param) {
if(!is_null($param)){
return (boolean)$param?"true":"false";
}
return "null";
}
foreach($test as $row){
print $row . " :: ". tester($row)."\n";
}
Exampled Output:
:: null 1 :: true :: false string :: true 45 ::
true
If you want to return the actual type rather than the textual representation this can be easily achieved with a slimmed down version:
Solution:
function ($param) {
if(!is_null($param)){
return (boolean)$param;
}
// not required but useful to keep for code clarity.
//return null;
}
Possible results:
1 (true) 0 (false) null

Related

How to make a PHP structure, that doesn't complain about requests of undefined properties?

I have a bunch of optional settings and I'm sick of checking for isset and property_exists.
In Laravel, if I ask for a property that does not exist on a model or request, I get null and no complaints (errors). How can I do the same for my data structure.
If I try array, I can't do simple $settings['setting13'], I have to either pre-fill it all with nulls or do isset($settings['setting13']) ? $settings['setting13'] : '' or $settings['setting13'] ?? null. If I try an object (new \stdClass()), $settings->setting13 still gives me a warning of Undefined property.
How can I make a class such that it responds null or an empty string whenever it is asked for a property that it doesn't have?
Simply do what Laravel does, create a class that deals with your data structure which returns a value if key exists, and something else if it doesn't.
I'll illustrate with an example class (this class supports the "dot notation" of accessing array keys):
class MyConfigClass
{
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function get($path = '', $default = null)
{
if(!is_string($path))
{
return $default;
}
// There's a dot in the path, traverse the array
if(false !== strpos('.', $path))
{
// Find the segments delimited by dot
$segments = explode('.', $path);
$result = $this->data;
foreach($segments as $segment)
{
if(isset($result[$segment]))
{
// We have the segment
$result = $result[$segment];
}
else
{
// The segment isn't there, return default value
return $default;
}
}
return $result;
}
// The above didn't yield a result, check if the key exists in the array and if not - return default
return isset($this->data[$path]) ? $this->data[$path] : $default;
}
}
Use:
$my_structure = [
'url' => 'www.stackoverflow.com',
'questions' => [
'title' => 'this is test title'
]
];
$config = new MyConfigClass($my_structure);
echo $config->get('url'); // echoes www.stackoverflow.com
echo $config->get('questions.title'); // echoes this is test title
echo $config->get('bad key that is not there'); // returns null
There is also a possibility to create wrapper as Jon Stirling mentioned in a comments. This approach will allow to keep code clean and also add functionality via inheritance.
<?php
class myArray implements ArrayAccess {
private $container;
function __construct($myArray){
$this->container = $myArray;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
public function offsetGet($offset) {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
$settings = array("setting1"=>1,"setting2"=>2,"setting3"=>3);
$arr = new myArray($settings);
echo $arr['setting1'];
echo "<br>";
echo $arr['setting3'];
echo "<br>";
echo $arr['setting2'];
echo "<br>";
echo "------";
echo "<br>";
echo $arr['setting4'] ?:"Value is null";
!empty($settings['setting13']) ? $settings['setting13'] : ''
can be replaced with
$settings['setting13'] ?: ''
as long as whatever you want to print and whatever you want to check exists is the same expression. It's not the cleanest thing ever - which would be to check the existence of anything - but it's reasonably clear and can be chained :
echo ($a ?: $b ?: $c ? $default ?: '');
However, you are not the first who are "sick of checking for isset and property_exists, it's just that we still have to do it, or else we get unexpected results when we expect it the least.
It's not about saving time typing code, it's about saving time not debugging.
EDIT : As pointed in the comments, I wrote the first line with isset() instead of !empty(). Since ?: returns the left operand if it's equal to true, it's of course uncompatible with unchecked variables, you have at least to check for existence beforehand. It's emptiness that can be tested.
The operator that returns its left operand if it exists and is different from NULL is ??, which can be chained the same way ?: does.
Admittedly not the best way to do this, but you can use the error suppressor in php like this:
$value = #$settings['setting13'];
This will quitely set$value to NULL if $settings['setting13'] is not set and not report the undefined variable notice.
As for objects, you should just calling for attributes that are not defined in class.

use the functions without its parameters

I am using 2 regex functions here and I wanna make another function which returns false when the 2 regex are both false and if not, then true.
The problem here is when I wanna use the 2 regex functions in the third one, I have to give them parameters, which is not necessary I think, because the third function will only return a simple true or false. I get an undefined variable whenever I give parameters to the 2 regex functions in the 3rd one.
I tried using global variables which works but since its a bad practice I am looking for a better solution.
Code:
function regex1($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function regex2($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function checkBoth()
{
if (regex1($input) === false || regex2($input) === false)
{
return false;
}
else
{
return true;
}
}
EDIT:
The checkBoth function I am using in my other file like this together with the other 2 regex functions:
if (!regex1($input))
{
// show error at the same time
}
if (!regex2($input))
{
// show error at the same time
}
if(checkBoth())
{
// success
}
function regex2($input,$secondVar=false)
{....
Later in code in place where you need just add:
if($secondVar !== false){
// do whatever...
}
If you can't user "false" you can just empty string '' or any other value that will not appear there.

Break array_walk from anonymous function

Is there a way to stop an array_walk from inside the anonymous function ?
Here is some sample code (that works) to show what I mean, that checks if an array has only numeric values.
$valid = true;
array_walk($parent, function ($value) use (&$valid) {
if (!is_numeric($value)) {
$valid = false;
}
});
return $valid ? 'Valid' : 'Invalid';
If I have a big enough array, and the first entry is invalid, the rest of the (redundant) checks are still done, so I would like to stop the execution.
Using break / continue doesn't work (error: Fatal error: Cannot break/continue 1 level in ...).
Note: I don't want to rewrite the code, I just want to know IF this is possible.
As stated, theoretically it's possible but I'd advise against it. Here's how to use an Exception to break out of the array_walk.
<?php
$isValid = false;
$array = range(1, 5);
try {
array_walk($array, function($value) {
$isAMagicNumber = 3 === $value;
if ($isAMagicNumber) {
throw new Exception;
}
});
}catch(Exception $exception) {
$isValid = true;
}
var_dump($isValid);
/*
bool(true)
*/
You can put a static flag inside the anonymous function:
array_walk($ary, function($item) {
static $done = false;
if($done) {
return;
}
// … your code
if($myBreakCondition) {
$done = true;
return;
}
});
This doesn’t actually stop the iteration, but all further cycles after the flag is set simply do nothing. Not very efficient, but it might work without any greater performance impact if the arrays iterated are not too large.
In your case, the code would be:
$valid = true;
array_walk($parent, function($value) use(&$valid) {
static $done = false;
if($done) {
return;
}
if(!is_numeric($value)) {
$valid = false;
$done = true;
return;
}
});
return $valid ? 'Valid' : 'Invalid';
But actually it won’t be much difference if there was no “break” at all. Only the “false” would be assigned for every invalid value, which does not matter as the result would be still false. Maybe it would be even more efficient that my static variable cheat.
Personally in your case I would use array_filter instead:
$valid = count(array_filter($parent, 'is_numeric')) == count($parent);
or just
$valid = array_filter($parent, 'is_numeric')) == $parent;
If all values in the $parent array are numeric, they would be all present after the filtering. On the other hand, any non-numeric value in the array would affect the contents (decreasing the item count) in the filtered array and the comparison would yield false.

Can a function have multiple return value types in PHP?

Is there a way I can have my PHP function return a different value type?
The following code should explain what I mean:
<?php
public function test($value){
if($value == 1){
return "SUCCESS";
} else {
return false;
}
}
?>
On one condition I'm returning a string, otherwise I'm returning a Boolean. Is that allowed/possible?
Yes. PHP is loosely typed, so any variable can take any type. Function return values are no exception.
Yes, it's possible (but not advisable).
You should at least declare it in your Javadoc comment
/**
* #param int $value
* #return mixed string|boolean
**/
public function test($value){
if($value == 1){
return "SUCCESS";
} else {
return false;
}
}
Even PHP's built in functions do it sometimes (e.g mysql_connect => boolean | resource link)
It is allowed. However, it is not advisable in the case you gave. For an example where this makes sense, take a look at e.g. strpos() function.
Yes, that's allowed, not like in C, Java, ..., where you have to define the return.
I wonder about this myself, why return something that is different? So the receiving end gets two things to check?
I think I have settled on returning an empty version of the type or an array with a status element myself.
Two examples, how things can change within a function/method, and you are basically forced to check more than expected:
<?php
function test($value) {
if ($value == 1) {
$local = successFunction($value);
return $local;
}
else {
return false;
}
}
$res = test(1);
if ($res) {
echo "Are we OK? Right? No? What is this?";
}
Version 2
function test($value) {
if ($value == 1) {
$local = successFunction($value);
return $local;
}
else {
return "";
}
}
$res = test(1);
if (!empty($res)) {
echo "More likely we are OK.";
}
?>
Is that allowed/possible?
Yep.

PHP return false with a string

I am looking for the correct way to handle a return statement with a bool/string. For example I do all my checking inside the function and return true if it all passes. However if something went wrong I would like to return a string of what went wrong rather than just return false; with a general string. Does php assume false if a var is set to anything besides true? What is the correct way to handle this? Here's an example of what I'm doing
<?php
$a = 2;
$result = CheckVar($a);
if ($result)
{
echo 'Correct!';
}
else
{
echo $result;
}
function CheckVar($var)
{
if ($var == 1)
{
return true;
}
else
{
return 'This is not the correct answer. You supplied '.$var;
}
}
?>
It seems this method works, however is this good programming etiquette? Or is there another way I should be doing this? Thank you for your time.
Does php assume false if a var is set to anything besides true?
Not at all. PHP will return whatever the variable was set to. And actually since you have a non-empty string, that's a "truthy" value (ie: true in a boolean context). Since you used if ($result) as your check and you return a "truthy" value, the condition is always true. You need to change that check to:
if ($result === true) {
...
What is the correct way to handle this?
I think it's a good enough way to handle it. An alternative would be to pass an error string variable by reference, and have the fail part of your code fill that, eg:
function check($var, &$error) {
if ($var == 1) {
return true;
} else {
$error = 'This is not the correct answer. You supplied ' . $var;
return false;
}
}
Some native PHP functions behave like this (eg: exec().) Yet another alternative is to return an array with the errors, like Jared suggested. I personally use this option when I expect multiple errors (eg: a form validation routine):
function check_stuff($stuff) {
$errors = array();
if (!$condition1) {
$errors[] = 'Condition 1 failed';
}
if (!$condition2) {
$errors[] = 'Condition 2 failed';
}
return $errors;
}
Now you can also take advantage of the fact that empty arrays are falsy:
$errors = check_stuff($your_stuff);
if (!$errors) {
echo 'No errors!';
} else {
print_r($errors);
}
You can use === to check if the returned value is boolean true. === checks the type as well the value.
if ($result === true)
{
echo 'Correct!';
}
else
{
echo $result;
}
I came up against this recently, my function would either return an error message as a string or return true like this:
function check_something(){
if(condition){
return 'error message';
}
// if we got this far all is good!
return true;
}
I would call it and check the outcome like this:
$var = check_something();
if($var !== true){
// $var is not boolean true, so it must be a string
echo $var;
}
This checks that the outcome of the function is not just a truthy string, but is explicitly a boolean true
This could be useful to someone returning true or returning false as a string.
if (is_bool($result))
{
echo 'Result is a true bool';
}
else
{
echo $result.'returning a string';
}

Categories