Jump to content

[SOLVED] I need some informatio about functions!


samoi

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.