Jump to content

Pulling A Single Varible Out Of A Fanction Help


50r

Recommended Posts

hello guys i am trying to get $error from this function to use it in another function bellow how do i do that?

$required = array('fname','lname','dbirth','email','password','address1','town','country','userName','pword1','pword2','cnumber');
function isEmpty($arrayRequired){
$error = array();
if(isset($_POST['submit'])){
 foreach($arrayRequired as $required){
  if(!isset($_POST[$required])  || empty($_POST[$required])){
   $error[]= $required;
  }
 }

}
}

 

function displayError($fieldName){
global $error;
if(in_array($fieldName , $error)){
 $mesage = "<span class='formError'>! This field connot be empty</span>";
}
 return $mesage;
}

Link to comment
Share on other sites

You could do it with globals as you are trying to do, but that is not a good way to go about it. In that case, you would have to define $error as global in isEmpty(), though. What you want to do instead is to return the array of errors, like this:

 


$required = array('fname','lname','dbirth','email','password','address1','town','country','userName','pword1','pword2','cnumber');

function isEmpty(array $arrayRequired) {
$error = array();

if (isset($_POST['submit'])) {
foreach ($arrayRequired as $required) {
if(!isset($_POST[$required]) || empty($_POST[$required])) {
$error[] = $required;
}
}
}

return $error; // Add this
}

 

Then, in your displayError method, you can call the above method and get the error messages, like this:

 


function displayError($fieldName) {
$errors = isEmpty(array($fieldName));

if (in_array($fieldName, $error)) {
$mesage = "<span class='formError'>! This field connot be empty</span>";
}

return $mesage;
}

 

You will probably have to restructure your methods a bit, but hopefully you get the point. :)

 

PS - Does anyone know how to indent code within code blocks on this forum?

Edited by Andy123
Link to comment
Share on other sites

I think that if you use the full editor (not the quick editor) the indentation is kept as is.

 

Edit: That is until you click on any buttons, such as the preview button, then the posted source has the tabs removed.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

@

Andy123 hey it worked but it is confusing when you define valuable errors its kind of we are now working on a new value not one that is coming from the isEmpty() function.

 

please if you dont mind explaing to me how this worked so i can be able to use it next time. i was assuming that every thing that the isEmpty() did was stored in the $error[] and i only had to find out a way of calling $error out of the isEmpty()?

another question is is that how i would call a valuable from a function?

Link to comment
Share on other sites

@

Andy123 hey it worked but it is confusing when you define valuable errors its kind of we are now working on a new value not one that is coming from the isEmpty() function.

 

please if you dont mind explaing to me how this worked so i can be able to use it next time. i was assuming that every thing that the isEmpty() did was stored in the $error[] and i only had to find out a way of calling $error out of the isEmpty()?

another question is is that how i would call a valuable from a function?

 

When you make a function call like this:

 

$myValue = someMethod();

 

... then $myValue contains the return value of SomeMethod(). If, for instance, someMethod() looks like this:

 

function someMethod() {
return 123;
}

 

... then the return value (which in this case is 123) would be assigned to $myValue. Therefore you do not have to keep a lot of variables out of your functions' scopes (e.g. as instance variables/properties in a class) or globals, which is great, because that is very hard to maintain.

 

I think that what you find confusing is the naming. In your own example, you have a method/function named "isEmpty". In this method, an $error variable is initialized. This variable is, however, local, which means that it only exists within the scope of the function. You can therefore not access it from anywhere else unless you return it or make it global, for example. So, once isEmpty() has finished executing, $error cannot be referenced anymore, so that is why you have to assign the return value of the function to a new variable. This variable is in your case $errors inside the "displayError" function. You can give this variable pretty much any name - even $error. This variable then exists within the scope of displayError(). That is, when this function finishes its execution, it is gone.

 

If you do not understand what I mean, please ask again. I hope my explanation is OK.

 

As for your last question, I am not entirely sure what you mean. If you want to access a variable that you define inside of a function, then you have a few options. You could use globals or sessions, but I would strongly discourage you from doing this. But it is still an option. If you are using a class, you could also make it a property, like this:

 


class Person {
private $firstName;
private $lastName;

function someMethod() {
$this->firstName = 'Andy';
}

function printFirstName() {
echo $this->firstName;
}
}

 

The code doesn't make much sense, but the point is that both of the methods access the same variable because it is defined within the scope of the class. Using $this accesses the current object instance.

 

You could also use a technique called "Pass by Reference". What this means that a parameter that you pass to a function allows the function to edit the source parameter. By default, a copy of the parameter is created, which is then used in the function. See the example below.

 


function add10($number) {
$number += 10;
}

$myNumber = 1;
add10($myNumber);

echo $myNumber; // Prints 1, not 11!

 

The add10 method is called with the value of $myNumber, which is 1. This value is then copied for use in the function. That is, if you change $number, this does not affect $myNumber. This is not the case if you pass by reference. Consider the following example.

 


function add10(&$number) {
$number += 10;
}

$myNumber = 1;
add10($myNumber);

echo $myNumber; // Prints 11, not 1!

 

The examples are almost identical, except for an & in front of the parameter name. This shows that the parameter is passed by reference. Any change you make to $number in the function affects the variable that you used when calling the function ($myNumber). Therefore 10 is added to $myNumber, which was initially 1, and thus it will be 11.

 

You can read more about passing by reference right here.

 

Hope this helped. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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