• 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

Spam Filter in Contact Form

Accessors (Getter) & Mutators (Setter) On a Pivot Table in Laravel

Get sql.h error on MacOS 12 (Monterey) sqsrv and pdo_sqsrv drive installation

"cannot list resources" error from WooCommerce REST API

Correctly determine if date string is a valid date in that format

printing all running session variable in laravel 5.1

Show Custom Data in Woocommerce Order Details Admin Area

Return multiple values from a function using mysqli_fetch_assoc [closed]

How to get rid of ampersand using WP Nonce URL and WP Redirect or PHP header

Symfony Error: "An exception has been thrown during the rendering of a template"

How to Create WooCommerce Subscription Product via. REST API?

Yii2: How to download backup files using spanjeta/yii2-backup?

How to increase the PHP upload limits [duplicate]

php echo remove slashes from url [duplicate]

How to Display Data in Yajra Datatables Laravel 7?

About Contact Privacy policy Terms and conditions