PHP is_countable vs. ?: [] [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 21 hours ago.
This post was edited and submitted for review 20 hours ago.
Improve this question
I am currently using a construct like
foreach ($some_var ?: [] as $bar) {
// ...
}
or
if (count($some_var ?: [])) {
// ...
}
to work with variables that are either an array or can also be NULL / not set.
I know that I could use "is_countable()" but this makes the code much longer and more unreadable.
Is the use of "is_countable()" equivalent with the "?: []" stuff? I cannot see any differences (except for some special varlues in "$some_var" that never will arise).
I do not understand the overhead of using a function like "is_countable()" along with the code line overhead which makes it unreadable.
Can you give me an example when is_countable() is necessary or "better" as to the PHP standards?

Related

What's the difference between using PHP define constant and PHP $_GLOBALS? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I read somewhere that defining constants with PHP's define function like so:
define('BASE_PATH','/var/www/html/example/');
is better and more secure than storing the same variable data inside a globals variable like so:
$_GLOBALS['BASE_PATH'] = '/var/www/html/example/';
Could somebody please explain the difference, which is better in which scenarios, and why?
I've just read here:
PHP Manual - The 'define' function
That the 'define' function can cause unwanted oddities.
a) What are the security implications of both?
b) How does PHP manage and store each of the variable's data in physical memory?

Why does PHP need the use operator for closures? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I know what the use operator is doing in something like function($x,$y) use ($z) { ...
What I don't understand is why PHP uses this construction when other languages don't?
Javascript has rather loose variable scoping (you don't need to declare variables as global). PHP has tighter variable scoping (if a variable isn't defined within the scope that it's used, and isn't brought in with global, then it doesn't exist).
The use declaration tells PHP to make those variables available within the closure (and likely also tells the garbage collector not to clean them up until after the closure gets cleaned up).

Is using the indirection operator twice in a statement a good practise? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Sometimes I see things like this:
<?php echo $this->getLayout()->createBlock("menupro/menu")->setGroup_id(5)->setTemplate("menupro/menupro.phtml")->toHtml(); ?>
I was wondering whether this is a good practise?
This is a rather subjective question, I personally am all for it. You can make your code a lot more readable. Check out this link:
Effects of method chaining
The major drawback is that you must return the object. You may not return any other value as you’re only allowed to return $this.

Why are php functions named so strangely? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
It sometimes feels like a drunk person wrote all the functions for php...Some are combined words with no underscore, other times functions are randomly underscored...Like using 'strtolower' and 'str_replace'...why does the former not have underscores (like str_to_lower) and the later does? and for that matter why is 'replace' the full word but 'str' isn't 'string'? If we are shortening words why not "str_rep"? Or better yet, why shorten anything and instead just make everything clear, obvious and readable, like "string_replace" and maybe consistently apply this to all functions in php? Is there some reason for why these functions are so strangely named? Is it just sloppy laziness in the language or do these apparent inconsistencies have some meaning?
Have a look at the history of PHP: http://en.wikipedia.org/wiki/PHP#History
It was developed organically by different people, what resulted in this inconsistent naming.

Why is PHP inconsistent? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Though, a best kick-start development language; but I don't know why PHP lacks of consistency in naming functions etc? I've been developing for years, but most often I miss-spell function names and forget their parameter structures. Why isn't there any standard conventions followed in PHP for naming? Some times, it's like substr and sometimes str_replace? I often forget if needle should be first argument or second? or haystack be first or second? Is team behind PHP working on developing a consistent conventions and names?

Categories