0

Hello I was helping a fellow daniwebber and I noticed that I have no idea if it is possible and how the syntax works for it. Any help appreciated.

example of how I think it works.

function retTrue(){
 return true;
}

//code in if
if(retTrue()){
 //do some php
}
3
Contributors
5
Replies
9
Views
7 Years
Discussion Span
Last Post by SikoSoft
0
function hasCaps($string)
{
    return(preg_match('/[A-Z]/', $string) > 0 ? true : false);
}

if(hasCaps('Raja')){
 echo "<br/><br/>Username is not allowed to have capital letters<br />";
}

if(hasCaps('raja')){
 echo "<br/><br/>Password is not allowed to have capital letters<br />";
}
?>
0

haha brilliant - not only did you help but you fixed the function in the other post :D

You sir are a genius!

0

You definitely can, and in my opinion: you SHOULD in most situations.

The reason I say you should in many situations is because you may be using less memory by not storing the result of the function call in a variable and just checking it in a condition right off the bat.

Of course, in many other situations, you will want to avoid doing this and instead store the result of the function call in an array, such as when you have many conditions to the statement checking the result. In these situations I'd recommend storing the result in a variable because otherwise it would invoke the function numerous times and cause serious overhead.

Like:

if ($type == 1 && myfunc())
{
// blah
}
elseif ($type == 2 && myfunc())
{
// bleh
}

The above statement is an example of when you don't want to do it, because if the condition is not met in the first "if", myfunc() is called ONCE MORE in the second condition. That's a scenario where you'd want to store the result in a variable and check the variable directly instead.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.