samoi Posted November 7, 2008 Share Posted November 7, 2008 Hi guys, I have a question about functions. I'm wondering if I can put if() statement inside a function? like this [BUT IT DID NOT WORK THIS WAY]. <?php require_once("samoi.php"); // which has the information of the database. $username = $_POST['username']; $password = $_POST['password']; function samoi() if(!$username || !$password) { echo "Insert inputs"; } else { $sql_insert = "INSERT INTO sam (username, password) VALUES ('$username', '$password')"; $qry_insert = mysql_query($sql_insert); echo "the data has been saved!"; } function samoi(); ?> any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/ Share on other sites More sharing options...
.josh Posted November 7, 2008 Share Posted November 7, 2008 functions require opening and closing brackets function blah () { ... } Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684737 Share on other sites More sharing options...
psymonic Posted November 7, 2008 Share Posted November 7, 2008 yes you missed the essential curly brackets Crayon Violent has said and yes you can nest as many if..else statements as you like inside and outside of functions and classes. Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684740 Share on other sites More sharing options...
sasa Posted November 7, 2008 Share Posted November 7, 2008 you must pass values to your function like this <?php require_once("samoi.php"); // which has the information of the database. $username = $_POST['username']; $password = $_POST['password']; function samoi($username, $password){ // add start function block and function variables if(!$username || !$password) { echo "Insert inputs"; } else { $sql_insert = "INSERT INTO sam (username, password) VALUES ('$username', '$password')"; $qry_insert = mysql_query($sql_insert); echo "the data has been saved!"; } } // end function block samoi($username, $password); //call function with values ?> Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684741 Share on other sites More sharing options...
samoi Posted November 7, 2008 Author Share Posted November 7, 2008 Thank you guys, you're always here to help I really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684747 Share on other sites More sharing options...
ashton321 Posted November 7, 2008 Share Posted November 7, 2008 What is the advantage to creating a function? Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684803 Share on other sites More sharing options...
.josh Posted November 7, 2008 Share Posted November 7, 2008 if you only plan on calling it once, then there's no advantage whatsoever. The point in putting something in a function is if you have a chunk of code being used over and over, you can just put a label on it and call the label. Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684844 Share on other sites More sharing options...
samoi Posted November 8, 2008 Author Share Posted November 8, 2008 Thank you all guys you helped me really there's no advantage, but I just want to make sure that a function can include if statements that's it. I have question for you all, I'm a beginner to the PHP, and I need your advice in how to learn it and be good at it!? I love it so much, but sometimes I get angry and sad that I cannot handle a simple script Don't suggest me to visit PHPmanual I'm sick of it! Thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684964 Share on other sites More sharing options...
.josh Posted November 8, 2008 Share Posted November 8, 2008 Sorry to hear that you're sick of the manual...it should receive some kind of award for best manual ever written (it probably has, multiple times). Quote Link to comment https://forums.phpfreaks.com/topic/131815-solved-i-need-some-informatio-about-functions/#findComment-684991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.