50r Posted November 9, 2012 Share Posted November 9, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/ Share on other sites More sharing options...
Andy123 Posted November 9, 2012 Share Posted November 9, 2012 (edited) 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 November 9, 2012 by Andy123 Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391361 Share on other sites More sharing options...
50r Posted November 9, 2012 Author Share Posted November 9, 2012 thanks for this quick one i will try it now Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391362 Share on other sites More sharing options...
Barand Posted November 9, 2012 Share Posted November 9, 2012 PS - Does anyone know how to indent code within code blocks on this forum? I wish! Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391363 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2012 Share Posted November 9, 2012 (edited) 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 November 9, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391364 Share on other sites More sharing options...
50r Posted November 9, 2012 Author Share Posted November 9, 2012 @ 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? Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391366 Share on other sites More sharing options...
Andy123 Posted November 9, 2012 Share Posted November 9, 2012 @ 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. Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391372 Share on other sites More sharing options...
50r Posted November 10, 2012 Author Share Posted November 10, 2012 If you do not understand what I mean, please ask again. I hope my explanation is OK. You can read more about passing by reference right here. Hope this helped. this isby far the best explanation that i have ever got from the internet. Quote Link to comment https://forums.phpfreaks.com/topic/270515-pulling-a-single-varible-out-of-a-fanction-help/#findComment-1391565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.