• 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

Pass an image through AJAX [duplicate]

api response laravel doesn't show

Symfony there are no commands defined in the "make" namespace

Currently Using CodeIgniter Framework i have an Error

getting error while enter Command => php artisan route:list

Target Individual Form Instance/Counter Inside A PHP While Loop

Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs

how to use extended ASCII instead of unicode in PHP

How can i call a function that executes an fpdf based on the choice of a form selector?

Get data from accuweather api url

How to set dynamic `home` and `siteurl` in WordPress?

Login if user is active using Laravel Breeze

How to Create WooCommerce Subscription Product via. REST API?

phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO)

Facebook API, get page post link (PHP)

About Contact Privacy policy Terms and conditions