Best 9 PHP Frameworks In 2019 For Test Automation

Best 9 PHP Frameworks In 2019 For Test Automation

In this article, we will compare the best 9 PHP frameworks in 2019 for test automation that eases the job of a tester and ensures faster deployment of your application.

PHP is one of the most popular scripting languages used for server-side web development. It is used by multiple organizations, especially for content management sites like WordPress. If you are thinking about developing a web application using PHP, you will also need one of the best PHP frameworks in 2019 for testing your application. You can perform visual and usability testing manually but for functionality, acceptance and unit testing, cross browser testing, an automated PHP framework will help pace the test cycles drastically. In this article, we will compare the best 9 PHP frameworks in 2019 for test automation that eases the job of a tester and ensures faster deployment of your application.

1. PHPUnit

PHPUnit is considered one of the best PHP frameworks in 2019 for test automation, meant for unit testing of an application developed using PHP language. Just like JUnit, it is an instance of xUnit and works almost in a similar manner. Sebastian Bergmann created the framework with the aim of assisting developers to detect mistakes in their coding.

Prerequisites For Installation

Apart from a proper understanding and development experience using PHP, you will need

  • The latest version of PHP installed in your system.
  • JSON and DOM extensions, which are by default enabled.
  • You will also require spl, reflection and pcre extensions. There are some extensions which are by default enabled.
  • Xdebug and tokenizer are required for code coverage. If you want to generate XML reports, xmlwriter is another extension that you need.

Why Should You Opt For PHPUnit As PHP Framework?

Just like other test automation frameworks meant for unit testing, PHPUnit helps you in developing a code that performs well and is easy to maintain.

  • It also helps you to identify defects that may arise before the code is pushed to further testing phases.
  • Issues are detected early during development phase since the testing is carried out by developers only.
  • Unit testing helps in detecting issues and fixing the code at a certain fragment of the application, thereby leaving other fragments intact and without any chance of breakage.
  • Debugging process is made simpler. Debugging is required only when a certain unit test fails.

Disadvantages Of PHPUnit

The only disadvantage of PHPUnit is that, for testing multiple functions, the developer is required to add cover annotations. By any chance, if you change the name of the method or function without updating the @covers annotation , testing is skipped for that certain method or function.

Is PHPUnit The Best PHP Framework In 2019 For Test Automation?

In the earlier days when test automation frameworks were not so much in use, PHPUnit, being the only player in the field was obviously the best and only choice for automated unit testing in PHP. But after the introduction of Codeception, opinions have changed. Codeception does unit testing in a relatively faster manner and using fewer resources.

Want to run your first automation script for PHPUnit? Here is a step-by-step guide for running an automation script on PHPUnit with Selenium.

2. Codeception

Codeception is an automation testing framework having multiple features apart from only Unit testing. The framework is derived from PHPUnit and has the ability to manage Functional, Unit as well as Acceptance testing of a web application. All these 3 are the main suites of Codeception and for additional testing, like API Testing, you can add API suite as well.

Prerequisites For Installation

You will need all the requirements for installing PHPUnit. Apart from that, you will be required to install a Composer to the root of your project.

Why Should You Opt For Codeception As PHP Framework?

There are many reasons which will compel you to try codeception.

  • First of all, you will get the best way to write test cases in PHP language. Syntax highlighting also eliminates the chance of mistakes in syntax.
  • Writing the test cases are very simple.
  • You can do not only unit testing but also others like Acceptance or functional testing just by adding suites.
  • Supports multiple frameworks without any need of extension or changing the code. If your project is migrating, your old test cases written using Codeception will work just the same way.
  • The Db module of Codeception helps the tester to fetch whatever you need from the database and clean up the data from your test module. Isolating the tests thus becomes much easier.
  • You can run complex test cases by using XPath locators or CSS selectors. REST and SOAP web services can also be tested simply using Codeception.
  • The tests are simple and readable enough to be clearly understood by a person coming from a nontechnical background.

Disadvantages Of Codeception

The only few disadvantages are

  • Since the framework emulates a browser, chances of getting false-positive result increases.
  • Requires a framework to start testing.
  • Cannot test AJAX and JavaScript.

Is Codeception The Best PHP Framework In 2019 For Test Automation?

As discussed in the previous section, when it comes to comparing unit testing framework, Codeception is much better than PHPUnit since it offers the option to perform acceptance testing as well as functional testing. However, if you look for acceptance testing only, then Behat is a much preferred option than Codeception.

Wish to get started with Codeception? Here is a guide to help you run your first Selenium script on Codeception.

3. Laravel Dusk

Laravel task is a browser-based test automation tool for testing PHP based web applications. This tool also helps you to automate tasks that are repetitive. Using this framework, you can either test the applications you developed or any other published website using Google Chrome.

Prerequisites For Installation

Installation and usage of Laravel Dusk are quite simple when compared to other test automation frameworks.

  • First of all, in the project you are developing, add a composer dependencylaravel/dusk
  • After the Dusk package is installed, run the command dusk: install.
  • By default, you can use Dusk only on Chrome. In order to test your application on other browsers, you will need to start a Selenium server.

Why Should You Opt For Laravel Dusk As PHP Framework?

For an application developed using PHP, testing it using Laravel Dusk is highly advantageous.

  • You can develop the best authentication and authorization script.
  • Presentation view like HTML and business logic like SQL queries can be easily separated.
  • The migration feature of Laravel allows you to create database tables of standard format.
  • For developers, Laravel can be used with Angular JS for developing large single page web applications.
  • The tool can also be used to create dynamic HTML elements since it supports vue js as well.
  • Asset compilation can also be done with elixir or gulp that minifies js and CSS files, resulting in the faster performance of the application.

Disadvantages Of Laravel Dusk

The only disadvantage of Laravel is that it cannot be used on other browsers except Chrome. Nowadays when customers demand a website that is cross-browser compatible, this is certainly a drawback.

Is Laravel Dusk The Best PHP Framework In 2019 For Test Automation?

The most important advantage of Laravel is that, apart from testing, it can also be used to develop web applications following the MVC pattern. However, if you are only looking at the testing perspective, Selenium is obviously the best choice for testing web applications, because of its ability to support multiple languages as well as its platform and browser compatibility.

Here is a complete document to help you perform automation testing with Laravel and Selenium.

4. Behat

Behat is a behavior driven testing tool for applications developed using PHP. The aim of the tool is to aid the nonverbal communication between stakeholders, management, and developers during the software development life cycle. It helps in documenting behavior examples of software when testing is carried out and how it matches the intended behavior.

Prerequisites For Installation

There are 2 ways of installing Behat

  • The easiest way is by downloading the latest version of behat.phar. You have to just place it in the project folder. Run the command $ php behat. phar -V and It will show you whether Behat has been installed correctly.
  • The recommended way is to use Composer, just like other test frameworks.

Why Should You Opt For Behat As PHP Framework?

Let’s list down the advantages first.

  • Test cases are written in a human-readable manner, the features describe the specs of individual sites and the test suite provides very clear documentation consisting of the web application’s features.
  • Helps in maintaining an application having a stable deployment cycle. All you need to do is run the tests, detect and fix the bugs and repeat the process until the project passes all the test cases, resulting in a stable application.
  • The acceptance criteria being easy to understand, developers get clarity regarding what the customer wants. Without any blockage, they can start developing and add new functionality. Once the functionality successfully passes the test cases, it gets ready for deployment.
  • It helps you to work closely with the customer until you two mutually agree on how a product should work. Thereby, forcing you to follow the standard protocols of behavior driven development.

Disadvantages Of Behat

In spite of all these advantages, there are certain drawbacks as well.

  • Behat takes quite some time for implementation.
  • You will also have to rethink and restructure the entire development process.
  • Discussion with stakeholders and developers may extend a lot until you come to a mutual understanding of how a feature will be developed.

Is Behat The Best PHP Framework In 2019 For Test Automation?

Although behavior driven test scenarios can also be generated using Codeception, however, it does not satisfy all the requirements of software developed using BDD environment. Since Behat is solely dedicated to BDD testing, there is obviously no other replacement for Behat other than PHPspec if you are developing a PHP based application.

Follow our guide for getting started with automation testing using Behat and Selenium.

5. PHPSpec

BDD is a development technique that allows a project to commence at specification and story level. PHPSpec is a popular BDD tool which helps you to write clean code using PHP. The development works by first describing how the object behaves. Once that is approved by the stakeholders, the next phase is to start coding.

Prerequisites for Installation

PHPSpec can be installed via 2 methods

  • Composer method
  • Using the Composer config file

Before starting the installation, make sure you have at least PHP 5.6 installed in your system.

Why Should You Opt For PHPSpec As PHP Framework?

Among the many advantages of PHPSpec, the ones that I found most useful are

  • While starting test case execution, PHPSpec generates skeletons of classes or methods based on the usage of the methods inside the test cases. Any additional edge that you face can be covered by writing an additional test case.
  • By refactoring, you can restructure the entire code without changing how it behaves externally.
  • You can also use PHPSpec to test applications developed using Laravel.
  • PHPSpec has a mocking framework known as prophecy. Prophecy ensures that for every test, the required class’s mock instance is passed to the constructor.

Disadvantages of PHPSpec

Apart from the advantages, there are certain disadvantages as well.

  • Testing private methods is a limitation.
  • Let’s suppose your public method returns a value. It is not a part of your object API. You cannot test that.
  • Integration testing cannot be performed using PHPSpec.
  • No Matter how much useful Prophecy is, it cannot mock abstract methods.

Is PHPSpec The Best PHP Framework In 2019 For Test Automation?

PHPSpec is mostly meant for designing classes. Without any need to get much involved in the infrastructure, you can design an isolated domain model. However, if you are looking for a tool that validates whether your application satisfies the requirement of the stakeholders, Behat is the first choice for testers and developers. Behat’s code is much easier to understand than PHPSpec, especially for people who are coming from a nontechnical background.

6. Atoum

Atoum is an automated framework meant for unit testing of an application that is developed using PHP language. Although it behaves a bit like PHPUnit, it is much more modern, simple and a standalone framework. Unlike PHPUnit, the test cases written using Atoum are easily readable and much simplified. Also, the asserters of Atoum enables you to write less code and test more functionalities.

Prerequisites For Installation

You can get the latest version of Atoum from their officialwebsite. There are several ways to install Atoum.

  • By usage of Composer
  • Cloning the repository at GitHub
  • You can download the PHAR archive from their official website
  • Atoum can also be used by integrating with other frameworks.

Why Should You Opt For Atoum As PHP Framework?

There are many advantages of Atoum which are not found in other PHP based unit testing frameworks.

  • It has a flexible structure. It allows you to rename directory, test cases or suites and whatever you like.
  • There are 3 engines for executing the test cases. For a single test case, you can define a specific engine apart from the engine provided by default.
  • You can write a test case in multiple ways. Either by using the classic way, by an academic way using empty asserters or a much better and smarter way by using the asserters of Atoum.
  • Test cases are readable and easier to understand since Atoum provides a set of natural assertions.
  • You are no longer required to manipulate real directories. While running the test cases, Atoum allows you to create a virtual file system consisting of fake directories, reducing the risk of mishandling real project files.
  • Class constructors can be mocked. Whenever a method is called, you can control the computational value.
  • The best feature is, Atoum can easily integrate with continuous integration tools like Gitlab, Jenkins, PHPCI, Travis CI, etc. It can also be used with multiple IDEs like Atom, Netbeans, Sublime Text, etc.

Disadvantages Of Atoum

When it comes to disadvantages, I haven’t found any which cannot be resolved or creates a roadblock.

Is Atoum The Best PHP Framework In 2019 For Test Automation?

The answer to this question is definitely yes. It is much better and flexible than PHPUnit or Codeception. Although it cannot perform functional or acceptance testing like Codeception, still when it comes to only executing unit test cases, Flexibility, multiple platform support and use of smart assertions make Atoum the best unit testing framework.

7. Kahlan

Just like Behat or PHPSpec, Kahlan is another behavior driven test framework. What makes it different is that, it takes BDD testing on PHP a step ahead by use of describe-it syntax. Just like Javascript or Ruby, using this framework, without any PECL extensions, you can monkey patch or stub your code directly.

Prerequisites For Installation

You will need a stable and latest version of PHP (at least version 5.5) installed in your system. After that, Kahlan can be installed in 2 ways

  • From a Git Repository. But for that, you need to have Composer and Git installed.
  • As a dev dependency, you can also install Kahlan from the composer package.

Why Should You Opt For Kahlan As PHP Framework?

Among the many advantages of Kahlan, the ones that I found useful are

  • Inclusion of JSpec, RSpec syntax
  • A much better code coverage metrics
  • You can check whether a specific method is called on your class.
  • Stubbing system is much more flexible. Prophecy or Mockery is no longer required.
  • You can replace the core classes and functions easily by monkey patching.
  • Icov and Istanbul are built in reporters that allow easy HTML or terminal reporting.
  • Coveralls, Clobber, Climate are some built in exporters that allow you to easily push the code.

Disadvantages Of Kahlan

The only disadvantage is that, Kahlan does not have the ability of Storytelling or developing scenario based BDD.

Is Kahlan The Best PHP Framework In 2019 For Test Automation?

Kahlan is no doubt a pretty useful BDD tool, but when we compare it with Behat, Behat is much more. A person who is not from a technical background, maybe a stakeholder or a business analyst can easily understand what a code written in Behat means. But for Kahlan, one needs to have basic understanding of PHP. If the project is in Agile, then also, Behat is a better choice.

8. Peridot

Peridot is a highly reliable, extensible PHP framework that supports testing of an application developed under behavior driven environment. The framework is lightweight and faster than PHPSpec or other frameworks. Also, the fact that it is event-driven makes it popular since developing plugins are much easier when you are using Peridot.

Prerequisites for Installation

For installation, you will need the latest version of PHP installed in your system. After that, Peridot can be installed by 3 procedures

  • Manually by downloading the latest Phar
  • Manually using the command prompt.
  • Using the composer like other frameworks.

Why Should You Opt For Peridot As PHP Framework?

Peridot has several advantages.

  • Just like Kahlan, it also uses describe-it syntax to write natural and human readable test cases. Test cases written using Peridot can be easily understood by someone belonging to a nontechnical background.
  • Provides a painless experience of writing ad hocs since it is event-driven.
  • You can also add custom reporters, helpers and plugins easily.
  • If your test cases are bulky, Peridot can run them concurrently since it is lightweight and faster than other frameworks.

Disadvantages Of Peridot

I did not find any such disadvantages while using Peridot. The only drawback is that it is not suitable to be used in an Agile application since its features are not vast like Behat.

Is Peridot The Best PHP Framework In 2019 For Test Automation?

Among the 3 BDD frameworks we discussed here, Behat is obviously the best choice. As mentioned earlier, the features of Behat helps the team during deployment as well as during acceptance testing. Although Peridot is faster and easier to understand, lack of supporting the application in deployment stages is kind of a drawback.


Selenium is the best automation framework for automated cross browser testing of a web application. For an application developed using PHP, you can use PHP bindings for developer browser-based automation test cases. The framework is flexible and the latest version of Selenium web driver runs flawlessly across all the major browsers.

Prerequisites For Installation

Selenium does not have any dependencies. However, if you want to use it with PHP, you will need a stable latest version of PHP installed in your system. You will also need a composer for providing the required dependencies.

Why Should You Opt For Selenium As PHP Framework?

There are countless advantages of using Selenium to test a PHP framework.

  • First of all the framework is open source. Anyone can download the source code and modify it according to project requirements.
  • It supports multiple programming languages. Even if your project someday migrates from PHP to any other technology, you can still use Selenium for executing the test cases.
  • Selenium can be used on any operating system and it supports all the major browsers like Chrome, IE, Opera, Safari or Firefox.
  • By using very less hardware resource, you can execute multiple test cases parallely using Selenium.

Disadvantages Of Selenium

There are certain disadvantages as well.

  • The framework is a bit difficult and test case creation takes a bit of time. The learning curve is also a bit steep since the framework is evolving day by day.
  • Since the software is open source, there is no reliable support if you face any complication.
  • Setting up the test environment is also complicated and also, it does not have any support for integration with test management tools in case you wish to report bugs.
  • You can only perform browser compatibility testing on the browsers available in your local machine.

Overcoming Limitations Of Selenium With LambdaTest Selenium Grid

LambdaTest went live with an on-cloud Selenium Grid to make automation testing easier for the tester community.

  • With LambdaTest Selenium Grid, you can perform Selenium based testing on more than 2000+ real browsers and browser versions.
  • LambdaTest offers integrations with numerous third-party tools to facilitate better bug-logging, continuous integration and continuous deployment pipeline.
  • LambdaTest provides their DesiredCapabilities Generator that would help to generate the desiredcapabilities class based on your selections via click operations.
  • LambdaTest offers 24×7 customer support through chat. You can also drop a mail to [email protected]for addressing any of your concern.
  • LambdaTest also launched API for Selenium, so you could extract custom and powerful reports of your Selenium script execution from LambdaTest platform.
  • Parallel testing lets you run numerous tests simultaneously, for trimming down your test execution even further.

Is Selenium The Best PHP Framework In 2019 For Test Automation?

If you are only thinking about testing a PHP based web application on the browser, Laravel Dusk is much more useful. However, if you are thinking about the bigger picture and want an open source automation framework that can perform cross-browser testing of a web application, there is no better choice than Selenium. Plus doing it on-cloud with LambdaTest you can hasten your test cycles as much as you want.

The frameworks mentioned above are not only for testers but for developers as well. Since, in the current age of DevOps and Agile, the traditional rule no longer prevails that testers will only do testing and developers will only do development. An exceptional tester or developer must have partial knowledge of the other domain as well. If you are a developer, the industry won’t expect you to be an expert in testing but you should have some basic knowledge of writing unit test cases to your code or understanding what the tester is explaining. The 9 tools mentioned above will help you a lot to progress if you are a PHP developer or tester. Even if you are a fresher, learning the popular unit testing or BDD tools among the ones listed above will also propel your career in the industry.

Originally published by arnabroychowdhury at

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Build a Simple REST API in PHP

☞ Developing RESTful APIs with Lumen (A PHP Micro-framework)

☞ Why I still use PHP in 2019 and why you should too.

☞ PHP Programming Language - PHP Tutorial for Beginners

☞ PHP for Beginners - Become a PHP Master - CMS Project

☞ Python and PHP Programming Bundle

☞ PHP OOP: Object Oriented Programming for beginners + Project

☞ Write PHP Like a Pro: Build a PHP MVC Framework From Scratch

☞ The Complete PHP MySQL Professional Course with 5 Projects

☞ Learn PHP Programming From Scratch

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Which is the best PHP Web Development Company in India?

Which is the best PHP Web Development Company in India?

Many experienced players prefer to give it a go ahead with PHP for their web development needs because of the simplicity that PHP provides. PHP is loaded with features that many other languages lack and those pre-defined features can bring new functionalities and finishing touch to your existing or new website that you are looking forward to create. Wouldn’t it be great to get the best PHP Web Development Company for your project; we have a reputation as few of the leading firms in IT industry when it comes to PHP development.

Perks you should keep an eye out for before considering to outsource project to any web development company:

1. Company’s Reputation- It is very important to know about the previous projects that the company has delivered and the success rate of those products. You can visit company’s website for client testimonials or can google reviews given to them by genuine clients. We follow transparency, you can contact our previous clients and let them tell you how their experience was while working with us.

2. Technical Skill set and Expertise- You should select a company that has multiple resource pool of expert and technically sound developers. Our developers are experienced with different PHP frameworks and are exceptionally good with writing bug free codes. You can also hire PHP developer in India from us, as our developers have on hand experience in PHP development (core and frameworks)

3. The Budget- Costing has always played as one of the most important roles in every business deal. We provide competitive pricing with no hidden cost guaranteed. With us you can expect your project to be in best quality with no compromise to be made even with the best price in the market.

Why Data EximIT is the best PHP web development company in India?

Here are some of the features that might love about Data EximIT:

 Reputation of Data EximIT is wonderful among the previous clients.

 We have around 87% of retention ratio of clients.

 We were time committed, means every milestone was achieved before or on time.

 Data EximIT follows Agile development so it was the best possible methodology.

 We have huge team with different expertise over multiple PHP frameworks, so you are provided with ample to options to choose from.

 We are very active in communication part. Method or time of communication was never an issue at Data EximIT.

 We have some of the best designers to ensure that your project not only functions but also looks and feels fabulous.

 We provided customers with best costing without compromising on the quality at all.

 We also provide dedicated PHP developer or team of PHP developers on hire for your project.

Data EximIT is where customers feel confident to come up with idea and leave with fully functioning & best in quality projects. You can also avail our developers at your requested site location if you decide to hire PHP developers in India from us.

Feel free to contact our experts and let success reach you by working with the best PHP web development company.

10 PHP String Functions for every Web Developer

 10 PHP String Functions for every Web Developer

10 PHP String Functions for every Web Developer

Originally published at on 8. May 2019

Every PHP developer should know functions like strlen()addslashes() and strpos(). In this Top 10 list you will find the most important ones of the approx. 100 string functions in PHP and learn how to use them correctly.

1. str_pad() – Fill in comma numbers

Let’s start right away with one of my favorite features. This function is interesting e.g. for IDs and strings, which must have a certain length, but don’t necessarily have it. Here is an example to understand it better:

$example = "706";

// Output: 70600000
echo str_pad($example, 8, "0");

// Output: 00000706
echo str_pad($example, 8, "0", STR_PAD_LEFT);

// Output: 00706000
echo str_pad($example, 8, "0", STR_PAD_BOTH);

// Output: 706-------
echo str_pad($example, 10, "-");

The function expects two parameters: The string to be changed and the expected string length. Optionally, the fill character can be specified as the third parameter. In addition, you can also specify on which side the string is to be filled. STR_PAD_RIGHT for right (default), STR_PAD_LEFT for left and STR_PAD_BOTH for both sides.

Complete documentation: here

2. explode() – Split string to array

If you often work with arrays and JSON data you won’t be able to avoid this function either. It can split your string into an array based on a specific character or whole string. This can be useful, for example, for URL parameters or file names:

$example = "jquery.min.js";

// split string at the . char
$splitted_string = explode(".", $example);

// Output: Array ( [0] => jquery [1] => min [2] => js )

// Output: File Extension: js
echo "File Extension: " . $splitted_string[2];

Optionally, a limit can be specified as the third parameter. This ensures, for example, that only two elements are present in the array when more is not needed.

Complete documentation: here

3. implode() – Merging arrays into strings

This function is the opposite of explode(). The function can reassemble an array with a certain separator to a string. You will use this function quite often in web development, because it is used for lists, numbers or SQL queries, for example:

$example = array("Tony Stark", "Peter Parker", "Bruce Wayne");

// Joins the array with a , char
$imploded_string = implode(", ", $example);

// Output: Top heros: Tony Stark, Peter Parker, Bruce Wayne
echo "Top heros: " . $imploded_string;

Of course you can also program this function yourself within a few minutes. But you don’t always have to reinvent the wheel.

Complete documentation: here

4. strlen() – Find out string length

Not to think away. This function is so versatile, e.g. it can be used to validate user entries or before entering them into the database in order not to exceed a certain string length. The function returns the length of a string. Spaces are also counted. Characters are counted. Here is an illustrative example:

// Output: 12
echo strlen("Hello World!");

// Output: 0
echo strlen("");

// Output: 1
echo strlen(" ");

// Output: 8
echo strlen("WebDEasy");

Complete documentation: here

5. strpos() – Examine for chars/sequences

This function checks whether a character or an entire character string occurs in a string. This function is often used, for example, to evaluate user input. Here are a few good examples:

// Output: 2
echo strpos("Hello World", "l");

// Output:
echo strpos("Hello World", "H");

// Output: 6
echo strpos("Hello World", "World");

// Check if the string contains 'World'
if(strpos("Hello World", "World")) { }

// Output:
echo strpos("Hello World", "Hello", 2);

In the last example, a third parameter is given. This parameter specifies an offset. This means that the function only checks whether the characters occur in the string from the second character onwards. A negative value in this parameter sets the offset of the string from behind.

Complete documentation: here

Pooh! This means we already have half of the top 10 PHP string functions. Second half:

6. addslashes() – Escape Strings

This function is extremely important from a safety point of view. Ever heard of SQL Injections? Good, because this function can have a part in preventing these attacks. Special characters, here (‘, “, , NUL) are masked here. This means that a backslash () is prefixed so that these characters do not enter the system unfiltered:

// Output: What's your name?
echo addslashes("What's your name?");

// Output: This is "Me"!
echo addslashes('This is "Me"!');

// Prevent SQL-Injection
// Output WITHOUT addslashes: INSERT INTO testuser (id, name) VALUES (1, ''; SELECT * FROM testuser;');
// Output WITH addslashes: INSERT INTO testuser (id, name) VALUES (1, ''; SELECT * FROM testuser;');
$name = "'; SELECT * FROM testuser;";
echo "INSERT INTO testuser (id, name) VALUES (1, '" . addslashes($name) . "');";

The difference in the SQL example is small, but can have devastating consequences!

Stripslashes() is the reverse function, the documentation can be found here.

Complete documentation: here

7. substr() – Create text excerpt

This function is known to all programmers because it exists in almost every programming language. This function returns an excerpt from the string:

// Output: Easy
echo substr("WebDEasy", 4);

// Output: DE
echo substr("WebDEasy", 3, 2);

You can specify the beginning of the extract and of course the length. I don’t think you have to say more about that. 

Complete documentation: here

8. str_replace() – Replace characters

This function can replace a certain occurrence of characters with other characters. This is useful for decimal numbers, for example, where you must distinguish between the German format (with commas) and the English format (with periods). Here is an example:

// Output: 20,000
echo str_replace(".", ",", "20.000");

// Output: 20.000
echo str_replace(",", ".", "20,000");

// Output: Ths s n str_rplc xmpl n php!
echo str_replace(array("a", "e", "i", "o", "u"), "", "This is an str_replace example in php!");

Arrays can also be passed as replacement parameters. This saves some function calls.

Complete documentation: here

9. print_r() – Function for everything

My absolute favorite function. Ok, admittedly not only meant for strings, but I would like to show them anyway. Beside the simple output of “normal” strings you can also output arrays. You can also write strings or arrays into the logs without losing the formatting. The second parameter must be set to true:

// Output: Hello World!
print_r("Hello World!");

// Output in log files: Hello World
error_log(print_r("Hello World", true));

// Output: Array ( [0] => This [1] => is [2] => an [3] => array )
print_r(array("This", "is", "an", "array"));

Complete documentation: here

10. strip_tags() – Remove HTML Tags

This function can output HTML code without tags. Sort of like the str_replace()function, except that you don’t have to pass each HTML tag individually. This is how it looks like:

// Output: Hello World!
echo strip_tags("<h1>Hello World!</h1>");

// Output: Hello World!
echo strip_tags("<div class="testclass"><p>Hello <b>World!</b></p></div>");

Complete documentation: here

The most important PHP string functions are now known to you. Which string functions are most important for your everyday life?

Here you can find a complete list of PHP string functions. I’m sure you can now do something with our other PHP posts as well:

Originally published at on 8. May 2019


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Ultimate PHP Basics for Absolute Beginners - [200+ PHP Code]

☞ PHP in Web Development in 2020

☞ PHP for Beginners - Become a PHP Master - CMS Project

☞ Learn Object Oriented PHP By Building a Complete Website

☞ MEVP Stack Vue JS 2 Course: MySQL + Express.js + Vue.js +PHP

☞ Object Oriented PHP & MVC

☞ PHP OOP: Object Oriented Programming for beginners + Project

☞ Learn PHP Fundamentals From Scratch

☞ The Complete PHP MySQL Professional Course with 5 Projects

☞ The Complete JavaScript Course 2019: Build Real Projects!