How To Convert Numbers To Strings In PHP


  • 21 Feb, 2012

    In a string context, PHP will automatically convert any numeric value to a string. Here is a PHP script examples:<?php print(-1.3e3);print("");print(strlen(-1.3e3));print("");print("Price = $" . 99.99 . "");print(1 . " + " . 2 . " = " . 1+2 . "");print(1 . " + " . 2 . " = " . (1+2) . "");print(1 . " + " . 2 . " = 3");print("");?>This script will print:-13005Price = $99.9931 + 2 = 31 + 2 = 3The print() function requires a string, so numeric value -1.3e3 is automatically converted to a string "-1300". The concatenation operator (.) also requires a string, so numeric value 99.99 is automatically converted to a string "99.99". Expression (1 . " + " . 2 . " = " . 1+2 . "") is a little bit interesting. The result is "3" because concatenation operations and addition operation are carried out from left to right. So when the addition operation is reached, we have "1 + 2 = 1"+2, which will cause the string to be converted to a value 1.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.