Free cookie consent management tool by TermsFeed Generator PHP Simple Tinker Like Script | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Simple Tinker Like Script

PHP Simple Tinker Like Script

Last Updated on Feb 15, 2023

Introduction

If you have worked with Laravel you know what tinker is but if you haven’t Laravel Tinker is a tool that allows users to interact with the application through the command line.

Today we are going to build a simple Tinker like tool that lets the user write php code in the command line.

So let’s get started

Simple Tinker

First we need a way to get whatever that user writes. There is a function for that in PHP and it’s called readline. It takes a string and with that prompts the user. For example

readline("what is your name? ");

Then the user is allowed to type whatever they want. And what they write will be returned by this function. So let’s prompt the user and store what they type in a variable

$code = readline(">>> ");

Great! Now that we have what the user wants to run, let's run it. How can we run a php code inside php?!

Easy. there is a function for it called eval. That runs the code that is given to it. For example:

eval("echo 2+2; ");

This will print 4. So let’s use it and run the code we got from user

eval($code);

Great. We are almost done. Right now when the application runs once it will be finished and comes out of our tool we want to keep asking the user and keep running the code. So let’s put our code inside a loop. Let’s also add a new line so the new prompt will be in the new line.

while(true){
    $code = readline(">>> ");
    eval($code);
    echo "\n";
}

Great. Now we keep asking the user and run their code. But what if the user writes a code that is not valid or there is a typo? Well php is going to throw an error and the user will be thrown out of the application.

So let’s handle the errors as well. Do you remember error handling?

Here is our final code

while(true){
    try{
        $code = readline(">>> ");
        eval($code);
        echo "\n";
    }catch(Throwable $th){
        echo $th->getMessage();
echo "\n";
    }
}

You can easily run this with your command line. You can write php <name of the file>

In my case it would be 

php learner.php

And to make it even cooler you can remove the .php from the end of your file. So my file would be learner and when I want to run the code I would run 

php learner

Here is a video of that

With a few lines of code we could create such a powerful application. Isn’t it amazing?

Conclusion

Now you know about creating a simple Tinker like application in PHP.

I recommend you to open a PHP files and try build your own. try to add more features.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • readline
  • eval
  • tinker like application
  • command line application

Category: programming

Tags: #php #open source

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP Validate Email
Feb 15, 2023 programming

PHP Validate Email

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the emails given by the user to make sure the email address has a correct format. ...

4 Min Read Read More
Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers
Sep 10, 2024 programming

Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers

While PHP and Python share several core programming concepts, transitioning between the two requires understanding the key differences in syntax, paradigms, and best practices. ...

19 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...