• 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

Fatal error: Trait 'LaminasDbAdapterLaminasDbAdapterAdapterInterface' not found in /var/www/vendor/laminas/laminas-db/src/Adapter/Adapter.php

MySQL order by field in Eloquent

disable two buttons after clicking another button

Laravel Collections. Is there some kind of assertStructure method?

PHP random string generator

Get all WooCommerce products within own plugin

Laravel insert dynamic input values with radio button

Android Volley getParams() method not getting called for JsonObjectRequest

PHP: convert all UTF-8 characters to HTML entities

WooCommerce - unset "<product> removed notice…" on cart page

Correct way to use LIKE '%{$var}%' with prepared statements?

MongoDB Duplicate Documents even after adding unique key

Creating live search with AJAX and CodeIgniter

In PHP, which is faster: preg_split or explode?

Transform array, set each array element with parent key php

About Contact Privacy policy Terms and conditions