4

How to Convert a String to a Number in PHP

 2 years ago
source link: https://www.laravelcode.com/post/how-to-convert-a-string-to-a-number-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Convert a String to a Number in PHP

  1073 views

  1 year ago

PHP

Use Type Casting

As we know PHP does not require or support explicit type definition in variable declaration. However, you can force a variable to be evaluated as a certain type using type casting.

Let's try out the following example to understand how this works:

<?php
$num = "2.75";
 
// Cast to integer
$int = (int)$num;
echo gettype($int); // Outputs: integer
echo $int; // Outputs: 2
 
// Cast to float
$float = (float)$num;
echo gettype($float); // Outputs: double
echo $float; // Outputs: 2.75
?>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK