is_array() distinction - php

I have a piece of code where a variable can either be an array or just a string.
if(!is_array($relation['display_name']))
{
// do something with $relation['display_name']
}
else
{
foreach($relation['display_name'] as $display_name)
{
// do the same with $display_name
}
}
This of course works - but it's not very nice. And I would have to do it a lot of times. Is there a better way of doing this?

You can do it like this:
foreach((array)$relation['display_name'] as $display_name) {
// do something with $display_name
}

You could do something like this:
if(!is_array($relation['display_name'])) {
$relation['display_name'] = array($relation['display_name']);
}
# do your foreach here

I would advice you look into fixing the source of the problem.
Why is $relation['display_name'] sometimes an array and sometimes not?
Problem fixing is better then patching the outcome.
That being said, I would create the following:
function transformToArray($mValue) {
return (is_array($mValue)) ? $mValue : array($mValue);
}

You could write it much shorter with the ternary operator:
foreach ((is_array($a) ? $a : array($a)) as $val) {
...
}

I think this is not a bad way of handling this issue. Most of the PHP code I have seen handles stuff like this similar.

If using >= PHP 5.3 you could try something like this. It will run the code on the element if it is singular or implicitly over all array members if an array.
function call($element, $func) {
if (is_array($element)) {
foreach($element as $value) {
$func($value);
}
} else {
$func($element);
}
}
call($relation['display_name'], function($display_name) {
// Anything you wanna.
});
CodePad.

Related

Simplify nested loops containing external variables

I'm trying to simplify a piece of PHP code looking like this :
public function gather($parameters, $infos)
{
$gathered = [];
$number = false;
foreach ($infos as $info) {
foreach ($parameters as $parameter) {
if (strlen($parameter['number'])) {
$this->mark($parameter['number'], $info, $parameter, $gathered); // $gathered is passed by reference
$number = true;
continue;
}
if (strlen($parameter['default'])) {
$this->mark($parameter['default'], $info, $parameter, $gathered); // $gathered is passed by reference
continue;
}
if ($config['smth']) {
doSomething(...);
}
}
}
if (number > 0) {
doSomething(...);
}
return $gathered;
}
Here are the problems I have :
These nested foreaches are really ugly and I'd love to the second one in the other function. But it uses variables that are created and used outside. I already used references with $gathered even if it's not really good.
The conditions of all ifs are different and are using different variables, do you know any way to generalize a case like this?
I am ok to rewrite big parts of the functions, so I'd be happy to hear other design ideas too.
[EDIT] The mark functions add elements to $gathered with some conditions and transformations.
[EDIT] Added more details.

How to skip lines in php

I'm struggling in finding a way to correctly do this logic.
If (this thing is null)
Skip it
Else
Don't skip it
I tried with if/else and while loops but each one will crash the program. I test something like this:
(inside a foreach)
if($value->getThing() == NULL) {
//HOW TO SKIP???
//I try to 'set' this thing
$value->setThing(0); //BUT IT Doesn't work because it's an associated object...
} else {
$value->getThing();
}
And tried this:
(inside foreach)
while ($value->getThing() != NULL) {
$value->getThing();
//Do Calculation...
}
Both just crash when it gets to the thing thats null. I know why but I can't figure out how to skip the null thing.
and if you can't tell, I'm a newbie. But I'm learning.
EDIT: The thing is null in the db.
Try this code :
foreach($values as $value){
if(!is_null($value->getThing())){
#do calculation
}
}
For "skipping" an entry you can use "continue".
foreach($array as $key => $value){
if($value['foo'] == null){
continue;
}
//Do the calculation
}
..or perhaps:
foreach($array as $key => $value){
if(is_null($value['foo'])){
//Null value treatment
continue;
}
//Do the calculation
}
What you are actually looking for is the NOT IS Operator as I like to call it.
foreach ($things as $thing) {
if (!is_null($thing)) {
// Do the stuff that you wanna do
}
}
The above dummy code teaches that you do not have to use an else. It also shows the is_null() function which checks if something is actually NULL. Furthermore it shows the ! operator that can also be translated to NOT IS.
What !is_null() actually says is: "If the return value of this function, variable and so on is not NULL..."
Good luck.
Try this:
$names = file('name.txt');
// To check the number of lines
echo count($names).'<br>';
foreach($names as $name) {
echo $name.'<br>';
}

Unset a variable without checking whether it has been already used or declared

To unset a variable without cheking whether it has been already used or declared.Which is valid in PHP: (a) or (b)? Although both works. In the sample code below,forward referencing is used,how PHP handles it internally?
(a)
while(stmt1->fetch())
{
unset($data);
$i=0;
while(stmt2->fetch())
{
//Some code.......
$data[$i] = $some_Value;
$i++;
}
}
(b)
while(stmt1->fetch())
{
if(isset($data))
{
unset($data);
}
$i=0;
while(stmt2->fetch())
{
//Some code.......
$data[$i] = $some_Value;
$i++;
}
}
Instead of unsetting the variable, set it with an initial value. This conveys the intention much clearer.
Also, you don't need to keep track of $i to insert a new element:
while ($stmt1->fetch()) {
$data = []; //Initialize empty array. This is PHP 5.4+ syntax.
while ($stmt2->fetch()) {
$data[] = $someValue; //$array[] means "Push new element to this array"
}
}
Method B is not neccessary. If you unset a non-existing variable nothing will happen, you won't get an undefined variable error.
You can see this behaviour here (it has error_reporting(E_ALL)).
Just in case you see it is some other code, I've already seen something like the following code (It's not the real code, only to give an idea of the use case).
The isset what used like "isset and with validated value". I'm not saying it's good practice (of course it's not, and even worse in this simplified example), it's just to show that, with overloaded magic methods, it may have some sense.
class unsetExample {
private $data = 'some_value';
public function __isset($name) {
if ($this->${name} != 'my_set_value') {
return false;
} else {
return true;
}
public function __unset($name) {
unset($this->${name});
echo 'Value unset';
}
}
$u = new unsetExample;
if (isset($u->data)) {
unset($u->data);
} else {
echo 'In that case I don\'t want to unset, but I will do something else instead';
}
Edit : Changed the code, it's much more how it was is the real code now

Can a function called in an equality comparison know what it's being compared against?

Is it possible to get string/int/function/or anything to compare in php function, if its used in if statement?
function return_str($str) {
$value = valueToCompare(); // I want to get "cat" here
if($str == $value) {
return $value;
}
else {
return false;
}
}
if(return_str('abc') == 'cat') {
echo 'No "abc" is not equal to "cat".';
}
Sorry if my function or example are a bit stupid, this should be part of larger script but I mimimized it.
Note: I don't want to hear Why wouldn't you just type if('abc' == 'cat'), because I must also handle multiple variables to check.
Do you have any ideas to achieve that? Please help me i'm not very good at these things.
How about using a second parameter?
<?php
function return_str($str, $compare) {
if($str == $compare) return $str;
return false;
}
?>
No, this is not possible, and is utterly pointless to try as well. If you want to compare a single string against multiple strings, why not
if (in_array('cat', array('dog', 'cat', 'zebra')) {
... cat's in there
}

function return code block query

Quick question to do with php functions, it may sound silly to some of you but I dont want to get in to bad habits. Is there anything wrong with doing the following?
function do_something($val)
{
$a = 1;
if ($val==$a)
{
return true;
}
return false;
}
Instead of;
function do_something($val)
{
$a = 1;
if ($val==$a)
{
return true;
}
else
{
return false;
}
}
Sorry guys I think my example isn't great. Basically the function could insert data into a database or send an email etc. With these functions I may only need to now whether it was successful or not by returning true or false. I wanted to know whether its suitable that I can use the shorter method instead of the if-else block.
I hope that makes it clearer.
Not really. Both works the same. However, it would be much cleaner to write it like this:
function do_something($val)
{
$a = 1;
return ($val==$a) ? true : false;
}
That's totally cool, because when returning a value, the function is left and it doesn't matter what follows.
But you could shorten this with
function do_something($val)
{
$a = 1;
return $val == $a; // this condition will be evaluated to true/false
}
The shortest way to do it:
function do_something($val)
{
return ($val==1) ;
}
No, that is perfectly fine, and in fact advised in multiple cases. :)

Categories