sanson06 Posted February 15, 2011 Share Posted February 15, 2011 Hello every one I just wanted to know how to call a function that is nested inside an if statement. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/ Share on other sites More sharing options...
Jessica Posted February 15, 2011 Share Posted February 15, 2011 Don't - put the function in a separate place. Functions should be kept together and organized. Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/#findComment-1174591 Share on other sites More sharing options...
Maq Posted February 15, 2011 Share Posted February 15, 2011 Not sure exactly what you mean. Can you provide and example? Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/#findComment-1174592 Share on other sites More sharing options...
sanson06 Posted February 15, 2011 Author Share Posted February 15, 2011 i don't understand what you mean so here is my code: <div style=" margin:auto; width:800px;"> <div style="float:left; width:800px; text-align:center; background:#000; color:#FFF;"> <h1>HEADER</h1> </div> <div style="float:left; width:200px; text-align:center; background:#333; color:#FFF; clear:right;"> <h1>Left</h1> </div> I want to call funtion HERE <div style="float:right; width:100%; text-align:center; background:#666; color:#FFF; clear:righ;"> <?php // Beginning of password script $password = "admin"; // Change Password to your need ?> <?php // If the password is valid user gets access if (isset($_POST["password"]) && ($_POST["password"]=="$password")) { function funtTest1(){ $hide1 = "hi 1"; echo $hide1; } function funtTest(){ $hide2 = "hi 2"; echo $hide2; } ?> <!-- Start of hidden content --> <br /> <span class="entry_title">title</span> <!-- End of hidden content --> <?php } else { // If wrong password or no password if (isset($_POST['password']) || $password == "") { print "<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b><br>Please enter the correct password</font></p>";} print "<form method=\"post\"><p align=\"center\">Please enter your password for access<br>"; print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>"; } ?> </div> <div style="float:left; width:100%; clear:both; text-align:center; background:#999; color:#FFF;"> <h1>FOTTER</h1> </div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/#findComment-1174595 Share on other sites More sharing options...
KevinM1 Posted February 15, 2011 Share Posted February 15, 2011 Defining a function is different than invoking a function. When you write: function myFunc($arg1) { // do something with $arg1 } You're not actually running (invoking) the function at that point. You're merely defining it. All you're really doing is telling the script "Here's a custom function I wrote." It makes sense to define functions outside of your script's execution flow in order to keep this distinction clear. Function definitions can still be placed in the same file as your script, but they don't belong in conditionals or loops. Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/#findComment-1174599 Share on other sites More sharing options...
sanson06 Posted February 15, 2011 Author Share Posted February 15, 2011 ok, so how would i do this? What i want is that when the user inputs the correct password i want to make the left side bar disappear and extend the container div. Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/#findComment-1174620 Share on other sites More sharing options...
KevinM1 Posted February 15, 2011 Share Posted February 15, 2011 It's as simple as (pseudo-code): if (/* entered password is NOT correct */) { // display side bar } There's no else-clause because I'm assuming that the default state of the page wouldn't have the side bar. Now, you'll have to figure out how to check that the user is logged in, how to pass that info from page to page, and how to display the side bar itself. Quote Link to comment https://forums.phpfreaks.com/topic/227772-function-within-a-if-statement/#findComment-1174687 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.