Solved

echo a hello

Posted on 2009-05-12
2
280 Views
Last Modified: 2012-05-06
hello there,
how can I make a php script that I can execute from the shell.. something like

/usr/bin/php /home/usr/website/script.php bob smith

and the script will display
'hello bob smith'

how can I do that?
0
Comment
Question by:XK8ER
2 Comments
 
LVL 7

Accepted Solution

by:
Jonah11 earned 300 total points
ID: 24369781
if the file below is saved as script.php, you execute it as:

>php script.php bob smith

and it will echo "hello bob smith"

you can alter it to account for single names only, or names with more than 2 words, etc
<?php
 
print 'hello ' . $argv[1] . ' ' . $argv[2];
 
?>

Open in new window

0
 
LVL 2

Assisted Solution

by:crash36936
crash36936 earned 200 total points
ID: 24369905
Jonah beat me to it...

These pages may help
http://us.php.net/manual/en/reserved.variables.argc.php
http://us.php.net/manual/en/reserved.variables.argv.php

Note that to use $argc and $argv, `register_globals` must be on.  Using $_SERVER['argc'] and $_SERVER['argv'] should always work.

Also note that if you run the script `php script.php bob smith`, then you're passing 2 arguments and will have to `echo` them all.  if you run `php script.php "bob smith"`, then the whole name is counted as 1 argument
<?php
    //note that $_SERVER['argv'][0] is always the script name
    echo 'Hello ' . $_SERVER['argv'][1] . PHP_EOL;
?>

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

831 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