mds1256 Posted January 26, 2011 Share Posted January 26, 2011 Hello I've have read that global variables should not be used. so if i have 2 .php files: index.php - main content php file sitefunctions.php - php functions site file on my index.php page i have the following (an example of my problem): <?php displayError($errorID); ?> and in the sitefunctions.php file i have the following (an example of my problem): <?php function displayError($errorID) { if($errorID == 1) { echo "Password Failure"; } else { echo "Other Failure"; } } ?> now that works but since when first accesing that page the $errorID is not set then i get an error. How can i achieve this without first setting a blank $errorID global variable? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/ Share on other sites More sharing options...
KevinM1 Posted January 26, 2011 Share Posted January 26, 2011 Don't call the function unless you have an error. A simple if-conditional should do the trick: if (isset($errorId)) { displayError($errorId); } Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165407 Share on other sites More sharing options...
mds1256 Posted January 26, 2011 Author Share Posted January 26, 2011 Don't call the function unless you have an error. A simple if-conditional should do the trick: if (isset($errorId)) { displayError($errorId); } Thats fine but if i cannot use global variables then how do i pass this $errorID over? Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165458 Share on other sites More sharing options...
KevinM1 Posted January 26, 2011 Share Posted January 26, 2011 Over to what? I think you're confusing variables in the global namespace (which was, until very recently, the de facto namespace for everything in PHP) and the use of the 'global' keyword. Pass variables into functions through their argument lists, and take care that they are defined (read: have a value) before attempting to use them. Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165461 Share on other sites More sharing options...
mds1256 Posted January 27, 2011 Author Share Posted January 27, 2011 Thanks for the reply. I will put the code together to show you what i mean then will post back. Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165921 Share on other sites More sharing options...
MasterACE14 Posted January 27, 2011 Share Posted January 27, 2011 function displayError($errorID=0) Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165928 Share on other sites More sharing options...
mds1256 Posted January 27, 2011 Author Share Posted January 27, 2011 function displayError($errorID=0) so i take it that 0 will be a default but what happens then if it is declared, will it take the declared errorID rather than the 0? Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165929 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 so i take it that 0 will be a default but what happens then if it is declared, will it take the declared errorID rather than the 0? no. you have to SEND the value to the function when it's called. if you don't send a variable, the default 0 will be used. // something causes an error, so you set the error id before calling the function $error_id = 123; // Call the function, passing the value displayError($error_id); // Note: the name of the passed variable is ignored by the function, so it can be anything. Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1165994 Share on other sites More sharing options...
mds1256 Posted January 28, 2011 Author Share Posted January 28, 2011 so i take it that 0 will be a default but what happens then if it is declared, will it take the declared errorID rather than the 0? no. you have to SEND the value to the function when it's called. if you don't send a variable, the default 0 will be used. // something causes an error, so you set the error id before calling the function $error_id = 123; // Call the function, passing the value displayError($error_id); // Note: the name of the passed variable is ignored by the function, so it can be anything. thanks! thats what im after! Quote Link to comment https://forums.phpfreaks.com/topic/225716-global-variables-and-function-arguments/#findComment-1166331 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.