Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-20
8
Medium Priority
?
2,336 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 2000 total points
ID: 39941877
does:
$_SERVER[ "argv" ]
contain some data?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

704 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