I'm trying to execute this code:
function main(){
if ($argc < 1){
listDir(".");
}
else{
for($i = 0; $i <= sizeof($argv); $i++){
listDir($argv[$i]);
}
}
}
But I'm getting the following error:
PHP Notice: Undefined variable: argc in /home/me/test.php on line 15
I thought that $argv and $argc were global variables. How can I get rid of this error?
I'm running this from command line.
Fix Notice: Undefined Variable by using isset() Function This notice occurs when you use any variable in your PHP code, which is not set. Solutions: To fix this type of error, you can define the variable as global and use the isset() function to check if the variable is set or not.
$argc — The number of arguments passed to script.
Introduction. When a PHP script is run from command line, $argv superglobal array contains arguments passed to it. First element in array $argv[0] is always the name of script. This variable is not available if register_argc_argv directive in php. ini is disabled.
There are two methods in PHP called $_POST and $_GET methods which are used to obtain the input from the user while using a form. While using forms in PHP, if there is any variable or constant with no values assigned to them, then an error is encountered called undefined index in a manner “Notice: Undefined index” .
Add a
global $argc, $argv;
after
function main() {
Those variables are in global scope, but not in your function's scope. The global keyword imports them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With