Solved

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

Posted on 2014-03-20
8
2,090 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 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