• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

Reading input in php from terminal

php


PHP Snippet 1:

function read_from_console ($prompt = '') {
    if ( function_exists('readline') ) { 
        $line = trim(readline($prompt));
        if (!empty($line)) {
            readline_add_history($line);
        }
    } else {
        echo $prompt;
        $line = trim(fgets(STDIN));
    }
    return $line;
}

$values = preg_split('/\s+/', trim(read_from_console()));

PHP Snippet 2:

php -q /path/script.php one two three

PHP Snippet 3:

array(4) {
  [0]=>
  string(10) "script.php"
  [1]=>
  string(4) "one"
  [2]=>
  string(4) "two"
  [3]=>
  string(4) "three"
}

PHP Snippet 4:

$first = $argv[1]; // it will be one 

Related Snippets

I want a way to give path to my files which are outside of public folder in laravel

Updating and Synchronizing Woocommerce Subscriptions to Custom Date

XAMPP keeps showing Dashboard/Welcome Page instead of the Configuration Page

Render the content of default_filter.php in Joomla front-end

Split array into 4-element chunks then implode into strings

Laravel SQL query midnight time not showing

PHP Startup: Unable to load dynamic library 'openssl' in Ubuntu

Get sum of arrays inside array

How to edit Records using CodeIgniter

Webscraping Symfony/Panther: Can't get HTML

Why does using salted hash on python and php give me different results?

If you intend you use SMTP, add your SMTP Code after this Line

PHP | "The requested PHP extension bcmath is missing from your system."

PHP: Insert marker every 3 iterations

How to remove from a multidimensional array all duplicate elements including the original?

About Contact Privacy policy Terms and conditions