Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a php function that returns true or false and works fine when I say:
if (is_valid_locale($locale)) {
// do something
}
Yet, when I am trying to find something that does not return a valid locale with this:
if (!is_valid_locale($locale)) {
// do something else
}
When I echo out the function, it does spit out "f" for false. So the function works.
I tried other things such as:
if (is_valid_locale($locale)==false) {
// do something else
}
However it still did not catch.
Am I doing something wrong?
If the function returns f, it doesn't return a boolean value.
As per convention, a function call is_something() should return a boolean value, so you should fix the function is_valid_locale() to return a boolean instead of trying to cope with what it's returning.
As soon as you've fixed that, your code should work as expected.
I think you should use === ,try this:
if (is_valid_locale($locale)===false) {
// do something else
}
Or if when you echo out the function, it does spit out "f" for false. Then try this:
if (is_valid_locale($locale)==='f') {
// do something else
}
If echoing the functions result gives you 'f' instead of nothing, you should change it to == 'f'.
Anyway, check what is the type of function result by using
gettype(is_valid_locale($locale))
In php False is equivalent to 0 not 'f'. Recheck you functions code or try.
if (is_valid_locale($locale)=='f') {
// do something else
}
Related
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 3 years ago.
Improve this question
I was analyzing some code and came to a part of it (simplified example) which says:
if ( !do_something( 'action-one' ) ) {
do_something_else();
}
Since I was seeing the output of this if statement on page i thought "OK, so do_something_else is why this part of page is showing. So I comment it out:
if ( !do_something( 'action-one' ) ) {
// do_something_else();
}
but it is still outputing same content.
Then I comment out the whole if statement, output dissapears.
So I realized that do_something was outputing that content which I wasn't expecting.
Shouldn't !do_something() just evaluate to true or false, especially because it has ! (expecting automatic type conversion)?
This seems like bad practice and something that should be avoided?
To know if !do_something() is true or false your server has to run the function (since you're asking it to run.) So echo's and prints will run and will show on your page. If you meant the function returns data: it shouldn't show up in this example (since you're not sending it to the client,) this would mean something more is going on than we can see here.
If you don't want to run the function in your if statement you should do something like this:
$do_somthing_bool = do_something('action-one'); //Function runs here
//This will still output echo's and prints but the output (return) of the function
// will be stored in $do_somthing_bool
if(!$do_somthing_bool) //Check if the value is false
//Do something
do_something does not return a bool in this case this is why you got a diffrent output
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to check if a filename of an image contains "cover". Somehow this stopped working for me (Pretty sure it worked already). I copied the part of my function not working.
$name=array("_IMG8555.jpg", "_IMG7769.jpg", "_IMG8458.jpg", "Cover.jpg", "_IMG7184.jpg");
$cov=array("Cover.png","Cover.jpg","Cover.jpeg", "cover.png","cover.jpg","cover.jpeg");
This does not work for me:
print_r(array_search($cov, $name)); //Returns empty String
print_r($name[array_search($cov, $name)]); //Returns first element of the name Array
Also I added a test Strings to make sure this is not result the searched string is the same as the search value.
print_r($name[3]===$cov[1]); //Returns true(1)
Can anyone help? Why does this simple script not work?
I also tried using in_array() but this is not working either.
The array_search() function search an array for a value and returns the key
array_search(key_value,array)
Loop your $cov array and get one key at a time and check with $name array
foreach($cov as $i => $cov_s){
if(in_array($cov_s, $name)){
return $name[array_search($cov_s, $name)];
}
}
return $name[0];
Try this code.
$name=array("_IMG8555.jpg", "_IMG7769.jpg", "_IMG8458.jpg", "Cover.jpg", "_IMG7184.jpg");
$cov=array("Cover.png","Cover.jpg","Cover.jpeg", "cover.png","cover.jpg","cover.jpeg");
foreach($cov as $c){
if(array_search($c,$name)){
//Do your success function
return true;
}
else
return false;
}
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
In PHP, the following yields the same result:
function bla1() {
return null;
}
function bla2() {
// nothing happening here...
}
So if I do this:
$bla1 = bla1();
$bla2 = bla2();
In both cases the value of bla1 and bla2 is in fact NULL. So my question is, which is best practice? Bla1 is more code, but makes it more obvious what you are doing. But bla2 is less code and yields the same result. Which is better?
If the function is meant to return something, for example: either an object if it exists, or null if it doesn't, then make it explicit by returning null.
If the function is not meant to return something, then don't make it return null. You can have a single return statement on its own if you need to exit the function early.
In most software development projects , the 20-80 rules applies . You will spend 20% of the time on developing your software and 80% of the time maintaining the software. With this rule in mind , Code Readability becomes so important in helping you and your colleagues maintain the software .
If you choose
function bla2() //not advisable
{
// nothing happening here...
}
You will plant doubts into the mind of colleagues of what is the actual function of the code when they review the code at a later date
I would be explict and choose
function bla1() //advisable
{
return null;
}
If the function is tend to return data, then return data or null when there is nothing to return. If the function doesn't meant to return any data back, then return true or false depends on if the function was executed successfully or not.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Ive asked a similar question before but i was really unclear so ive decided to use a more concrete example.
Does php save the result of the variable or does it save the procedure to run it? Why im wondering is if i store a function in it, does it store the return value or just copies the procedure
say:
function foo($something)
{
for loop
{
echo 'Something';
}
return $something;
}
$b = foo(5);
from what i encountered just assigning the value executes the function. Which i dont want because i dont want to go through double the for loops and do double what could be inside.
In PHP you can have both (either store result, or function's code)
if you write:
function foo()
{
return 5;
}
$a = foo();
this will mean - execute function foo and store result into $a
if you write:
$a = function()
{
return 5;
};
$a();
this will mean - store function's code into variable $a, then execute function stored in $a
PHP is a strict programming language, meaning that expressions are always completely evaluated. The line
$b = foo(5);
computes the value for foo(5) before the assignment; PHP does not leave it as a thunk to be evaluated when or if the variable $b is used.
If you want to you can achieve something similar to a thunk by creating a closure, like this:
$b = function() { return foo(5); };
This will not evaluate foo(5) until its value is needed, and then to get the value you must call the closure as $b().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
function myCheck($in)
{ return isset($in); }
$var1='Something';
$var2='$var1';
$var3='$varNonExitant';
What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:
myCheck($var2) return true;
myCheck($var3) return false;
isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.
To sum up: you cannot replicate it with a custom function.
Edit:
As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:
function variable_isset(&$variable = NULL) {
return isset($variable);
}
This approach offers two drawbacks though:
This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.
It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.
Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.
When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.
You have to give the function a string with variable name:
function isset_global($variable_name)
{
return isset($GLOBALS[$variable_name]);
}
Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.