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
}
Member Avatar for rajarajan2017

yes you can...

commented: answered very quickly +1
Member Avatar for rajarajan2017
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 />";
}
?>

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

You sir are a genius!

Member Avatar for rajarajan2017

Welcome!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.