Solved

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

Posted on 2014-03-20
8
2,168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39941877
does:
$_SERVER[ "argv" ]
contain some data?
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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
 
LVL 143

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 143

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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