Solved

function available outside function?

Posted on 2016-09-21
3
77 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
[X]
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
  • 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
 
LVL 1

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to count occurrences of each item in an array.

705 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