Solved

function available outside function?

Posted on 2016-09-21
3
41 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 42

Expert Comment

by:zephyr_hex
Comment Utility
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
Comment Utility
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 42

Accepted Solution

by:
zephyr_hex earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now