?
Solved

function available outside function?

Posted on 2016-09-21
3
Medium Priority
?
82 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 44

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 44

Accepted Solution

by:
zephyr_hex (Megan) earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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