Jump to content

[SOLVED] I need some informatio about functions!


Recommended Posts

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?

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
?>

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. 

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.