Solved

$argv and $argc is not working with php on command line

Posted on 2014-03-20
8
1,970 Views
Last Modified: 2014-03-20
Dear Experts,

I don't know why when I pass variable into php script on command line
that doesn't work, for example, the following test.php code. Nothing
is echo out.

php test.php 1 2 3 5

#!/usr/bin/php
<?php

echo $argv[1]."\n";
echo $argv[2]."\n';
var_dump($argc);

?>
I am using  php version as follows
 
PHP 5.3.21 (cli) (built: Feb  4 2013 12:36:43)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
    with the ionCube PHP Loader v4.2.2, Copyright (c) 2002-2012, by ionCube Ltd., and
    with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies

I also try php -f test.php 1 2 3 4  but it also fail.

Do I need to add some option or setting in php.ini before doing passing variable
on command line into php script ? Or is it related to the type of console shell I am
using ? I am using BASH linux shell on apache linux server and I am through my home computer  putty software connecting to remoter server.

Please advise

Duncan
0
Comment
Question by:duncanb7
  • 4
  • 4
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39941851
from the technical refernce:
http://www.php.net/manual/en/reserved.variables.argv.php
   Note: This variable is not available when register_argc_argv is disabled.  
so, please check you php.ini file:
http://www.php.net/manual/en/ini.core.php#ini.register-argc-argv
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39941869
realpath_cache_ttl      120      120
register_argc_argv      On      On
register_globals      Off      Off
register_long_arrays      On      On



register_argc_argv is ON from phpinfo() in php but still not working

Duncan
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39941877
does:
$_SERVER[ "argv" ]
contain some data?
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39941886
yes,  it is array with those variable value including "test.php"  ,1,2,3.

Probably I can use $_SERVER instead of $argv for solving the issue

But why the variable  is saved at $_SERVER, supposed it is used on Web browser
not on command shell php running  ?

Duncan
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39941945
would it work if you use
php-cli
instead of
php
0
 
LVL 13

Author Closing Comment

by:duncanb7
ID: 39941954
it is same and NOT working using php-cli  with $argv , and $_server is working
with both

If you have time, I am appreciated you can explain it between $argv  and $_server['argv']

Have a nice day

Duncan
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39941969
if the code was in a class module, you would need to declare the variable
global $argv;
first before using it, but other than that, I don't see why it isn't filled.
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39941992
Thanks for your reply

For my memo only:
1-Temporarily use $_server instead of $argv to solve the issue for continuing project
2- follow up the $argv on command line issue on next new thread later on.


Duncan
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now