The Proper Way to Measure Execution Time of a PHP Script

Hey there!

I’m going to give you a very useful snippet that you can use in all your php projects.

Execution time of a PHP Script is very important for numerous reasons.

The main reason that you should track your php script execution time is because Time matters when programming.

This can help you be more efficient in your programming and make sure your code runs smoothly and fast.

Instagram image from @motivational.coder

How to do it?

The idea is pretty simple, so, no worries!

You will need to define a variable at the very beginning of your project microtime() and one at the end.

After that you will need to subtract the end microtime variable from the start microtime variable and thats it.Also you can set the output value to only show the first 2 decimals.

 

Example

Lets take a quick example of a use case.

You are building a project and something is slowing you down and you want to find out what exactly first.

You can calculate this with the script above to measure the php execution time of your script programatically and start from there.

You can wrap this snippet around any php script and get your execution time for that specific portion.

Here is a good example on what you could do with it:

 

Some tips to improve performance

Here are some tips to improve your overall php scripts performance and get the best out of your project.

  • Make sure you use the latest stable release of PHP
  • Try to use less calls to the Database ( if using a database )
  • Refactor deprecated functions of php with new versions of those functions
  • Stay up to date with PHP and latest functions and methods to use

 

Thank you for reading, make sure to check out my socials below if you want to see more from my side 🙂

 

Check if MySQLi extension is enabled with PHP

Checking if the MYSQLi extension is installed and enabled is pretty straight forward. We can call the function_exists() on a mysqli function and that will give us true or false, depending on the server, if it’s installed or not.

This can be quickly adapted to other usages, like checking for the GD Library extension or anything else.

 

Easy PHP Script to check domain availability

So you are looking to check if your domain or others are available?

Check out our snippet from below and you can easily do that.

Here is some more information about it. Enjoy!

We are going to look at a very simple php snippet that lets you know if a specific domain is registered / available for purchase or not.

You could expand / grow this snippet and make your own website that checks websites domains for availability. To check if a domain is registered or not we will use the checkdnsrr function that PHP provides us.

This function searches for the DNS records of the specific website / domain and if they aren’t available that means the domain isn’t registered.

Here is a simple, lightweight php snippet to check Domain availability:

All you need to do is replace the value of the $domain variable with the one you want to check and you’re good to go.

For this example, the result / output is: grohsfabian.com is already registered !

 

OOP MySQLi : How to use Prepared Statements

Hi there guys ! In the last tutorial we talked about How to connect to your database via MySQLi and today we are going to discuss a very important topic in learning how to handle the new MySQLi.If you didn’t read the first article on MySQLi, I suggest you check it out before.

Why use Prepared Statements?

  • Security – You won’t need to escape characters when using prepared statements
  • Perfomance – The statement is prepared only once, but you can execute it multiple times
  • Possibly Cleaner Code – As you won’t need to escape the variables, you are writing less and more organised code

Downsides of Prepared Statements

The first downside of using prepared statements that I can think of it that you can echo out the query so that you can easily check if you created the query correctly or if you have some errors and you want to debug it, you can easily output it and see what’s wrong.As with the Prepared statements, you can’t echo out the query.Another downside is that it can be confusing at start if you don’t have much background in programming but that shouldn’t scare you.I encourage you guys to learn how to use prepared statements.

How to use Prepared Statements

Alright, so you’ve decided to learn how to use Prepared Statements, that’s a really good thing and will definitely help you in your future projects.
$database – variable which contains the OOP database connection.

prepare()

Alright, so you are probably asking what we just did.. When we prepare the statement, instead of using WHERE id = {$id}like we would’ve used normally we replaced the variable with a question mark ( ? ).If you have more than one variable, for example you have WHERE email = {$email} AND id = {$id}.All you need to do is replace all the variables from the query with ? so we would have this query WHERE email = ? AND id = ?.

bind_param()

What does bind_param() do? Well, remember the ? (question mark) signs that we just used ? I guess you already figured it out what it does, but I’ll tell you anyway.It binds / assigns every question mark you used with the prepare() function in the beginning with a variable.

It requires 2 or more parameters.The first parameter will always be a string which contains the type of variable you are sending to the database.I know it sounds weird but let me give you and example, examples are good.Lets say we have 2 variables that we want to assign, $email and $id.The email variable is a string so we will use the s character and for the id variable, which is an integer we’ll use the i character.Check out this example.

The s represents the type of the $email variable and the i represents the type of the $id variable.

I hope you learned something today, thanks for reading my tutorial.Stay tune for the part 2 where we learn to execute the statement and if needed get some results from the database.

 

 

OOP MySQLi : Connect to your database

Why should you use MySQLI ?

Firstly, MySQLi stand for: MySQL Improved. Since PHP 5.5.0 old mysql extension is deprecated and will soon be removed.This is the first reason you should use mysqli over mysql.

Secondly and what I think it’s the most important and useful feature that mysqli give us is that you can use prepared statements for your queries.That is a much much safer way of querying your database and preventing sql injection.

Connecting to your database using MySQLi

Create a new file named something like connect.php, database.php or whatever your want.Now open it with your favourite text editor.

First thing we need to do is defining the variables which contain information of your database, of course you don’t need to define them but it’s a much cleaner and will may be helpful later in your projects.

Great, we are now ready to proceed to actually connecting to the database using the new mysql improved extension by adding this variable.

After this you are pretty much all set and you can test it on your localhost or webhost and if you get no errors and the page is blank then you connected successfully to your database.

I personally like to let it as it is without checking for errors because if there is any error php will automatically tell you that something went wrong.For example if you have a larger PHP project and you setup your database connection wrong, you can easily tell because of all the errors you will get all over the page if the database connection doesn’t work.

Thats it for today’s tutorial, I will probably do part 2 very soon for you guys.Thanks for reading !

 

PHP Seconds to Days, Hours, Minutes, Seconds

Hi there guys, today I’m going to give you a little snippet that I found on the web which I find very useful and I thought I should share it with you.This function will require 1 parameter, the number of the seconds you want to transform into Days, Hours, Minutes, Seconds.

e.g: 600000 = 6 days, 22 hours, 40 minutes, 0 seconds

Hope you find it useful, I sure did !