Solved

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

Posted on 2014-03-20
8
1,936 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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