Posted inphp / Programming / Technology

PHP string to int | php cast to int

Today, We want to share with you php string to int.In this post we will show you php check if string is integer, hear for php intval vs int we will give you demo and example for implement.In this post, we will learn about PHP String Concatenation with an example.

Convert PHP string to int value

To cast a PHP string to int is quite easy. I required to use typecasting. Therefor you required to use (int) before your variable.

$variableName = (int)$stringName
$variableName = (float)$stringName

Here is an example of how to do this:

first of all To check if the source code really works I can use the === operator. This PHP operator checks not only values but types as well. Therefor the source code should look like this bellow:

One more question is open. What happens if our string is not a each number string. I mean there are other characters as well in the string.

In this Example, the cast operation tries the good as well as can cast the string if only spaces are there or if the not valid characters are and then the number value.

It works as bellows:

  • “8” -> 8
  • “8.5” -> 8
  • “8,5” -> 8
  • “8  ” -> 8
  • ”  8  ” -> 8
  • “10test” -> 8
  • “test10” -> 0

I hope you get an idea about php cast to int.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype