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

PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
What Modern PHP Looks Like in 2025
Jul 18, 2025 programming

What Modern PHP Looks Like in 2025

PHP has quietly evolved over the years, shedding many of its dated stereotypes while embracing modern programming practices and tooling. What used to be a language mocked for its inconsistencies and spaghetti-code reputation is now a mature, robust, and highly adaptable part of the web development ecosystem. ...

20 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More
Docker Beginner to Advanced 2025: The Complete Guide for Developers
Jul 18, 2025 programming

Docker Beginner to Advanced 2025: The Complete Guide for Developers

Containers have transformed the way we develop, deploy, and scale applications. Docker leads the charge, offering a developer-friendly platform to create consistent, portable environments from development to production. ...

27 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 ...