FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Stuck with a PHP Script.





defnet
Yo guys, I'm running through a bunch of tutorials and I got stuck on one page about sending mail with PHP.

This specific piece of code is used to check whether the message was successfully send.

Code:

if(mail($to,$subject,$body,$headers))
{
echo "An e-mail was sent to $to with the subject: $subject";
}
else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";
}


My question is: How is the mail function executed if it is the IF-condition?
Must the mail function be called in a previous statement, or does the mail condition return a boolean or other value?
mathiaus
http://uk.php.net/function.mail
Quote:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
SlowWalkere
To elaborate a bit on Mathiaus' point, any function can be called during an if statement.

Functions normally return a value when they are finished. In some cases, like time(), the function is returning some form of data that you will want to store in a variable. In others, as with mail(), the function simply returns true or false depending on whether the function was successful or not succesful.

When you call a function inside of an if statement, the function is run as normal and the value that it returns is evaluated in the if statement. So in your example, the mail() function is called, the script attempts to send an e-mail message, and either true or false is returned. If the mail() function is successful, it returns true and the 'if' portion is executed. If the mail() function encounters some kind of error, it returns false and the 'else' portion is executed.

Also, although mail() does return a boolean value, it doesn't have to do so to be useful in an if statement. If a function returns anything other than 0, null, or false, the if statement will evaluate as true. So you could use a function that returns a string if successful and null if not, or a function that returns an integer if successful and 0 if it is not, and so on and so forth.

- Walkere
defnet
Thanks guys, I have one more Question .
In order to use the mail function as the if-condition...
Do I explicitely need to call the mail function before I check it's reply in the if-statement or does the condition of the if-statement call the mail function.

eg.
Code:
if(x)
{blah;}

So now it checks if the value of x (the variable) is greater then a null value
Code:
if(x())
{blah;}

Does it now execute x (which is in this case a function) or does it also check if is is greater then a null value[/b][/code]

Do I have to run x before I start the if-statement?
salman_500
defnet wrote:
Thanks guys, I have one more Question .
In order to use the mail function as the if-condition...
Do I explicitely need to call the mail function before I check it's reply in the if-statement or does the condition of the if-statement call the mail function.

eg.
Code:
if(x)
{blah;}

So now it checks if the value of x (the variable) is greater then a null value
Code:
if(x())
{blah;}

Does it now execute x (which is in this case a function) or does it also check if is is greater then a null value[/b][/code]

Do I have to run x before I start the if-statement?


if you have already executed x before and not output any results... then you can use if statement with out puts..... e.g.

Code:
$y = x();

if ($y)
{
 blah
 }


actually its the same thing...in both cases it executes it in the if stement...

so when your doing :

Code:


if (x())
{
 blah
 }


its the same thing if u compare em......
defnet
Thanks guys for the advice.
I finally understand this thing completely.
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Inserting data to MYSQL with a PHP script
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Simple PHP script not working
PHP Script- How can we install it
I need a VERY simple php script
PHP Script needed
Logging systeem php script
Preventing bad PHP script calls
array check box on php script using $_POST
How to change extension php to html but the php script still
php script not loading
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.