Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

function available outside function?

Posted on 2016-09-21
3
65 Views
Last Modified: 2016-09-22
I am still trying to wrap my head around functions and scope.

I thought that you couldn't call a function from outside of a function yet I have taken an example from W3 Schools website

if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

Open in new window


Unless I am so confused I am missing something, it looks like:

 $gender = test_input($_POST["gender"]);

Open in new window


is being used outside of the actual test_input function and this works. I thought it wouldn't work because it's outside the function. Or am I misunderstanding?
0
Comment
Question by:Black Sulfur
  • 2
3 Comments
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41809678
The first segment of code is not a function.  It's a conditional statement, and as such, it is in the same realm / scope as the test_input function.

That being said, some IDE's will throw a warning because the function should be written above the point where it's called.  In other words, the function should appear before the conditional statement.
0
 

Author Comment

by:Black Sulfur
ID: 41810098
Yeah, I also thought it strange that they had the function at the bottom but that is how they did it.

Could I make it all into one function if I wanted to use it all as a form validation function?

function test_input($data) {
    
    $message = "";
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;

if (empty($_POST["gender"])) {
    $message .= "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
          }
    
    if($message) {
        
        echo "There were errors in your form:<br>" . $message;
        
    } else {
        
        //submit form
   }
     }

Open in new window

0
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41810964
No, this isn't going to work for two reasons.  First, line 7 will cause the control flow to exit the function and nothing below that line will run.  Second, line 12 is a recursive call to the function, which could cause an infinite loop.

I think you want something like:

$message = null;
function test_input($data, $text) {
   
    $innermessage = null; 
    if (!isset($_POST[$data]) || empty($_POST[$data])) {
        $innermessage = $text . " is required. <br/>";
        return $innermessage;
    } else {
        $_POST[$data]= trim($_POST[$data]);
        $_POST[$data] = stripslashes($_POST[$data]);
        $_POST[$data]= htmlspecialchars($_POST[$data]);
    }

}

//example call to function:
$message .= test_input("gender", "Gender");
$message .= test_input("foo", "Foo");
if($message !== null) {
    echo "There were one or more problems:<br/> " . $message;
} else {
    //submit form
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question