Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  Spam!  
Old August 4th, 2015, 08:01 AM
beingchinmay
Guest
 
Posts: n/a
Default How To Convert Numbers to Strings in PHP?

In a string context, PHP will automatically convert any numeric value to a String. Here is a PHP script examples:

Quote:
<?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:
-1300
5
Price = $99.99
3
1 + 2 = 3
1 + 2 = 3

The 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert list numbers to static text AlisonCan Word VBA 2 November 4th, 2014 10:40 AM
Convert Numbers to Words hasankelepir Classic ASP Basics 5 October 1st, 2007 07:15 AM
HOW TO USE STRINGS,ARRAYS AND VARIABLES IN PHP jhan316 Beginning PHP 1 July 25th, 2006 07:17 PM
how can i convert strings into integers amahja56 C++ Programming 2 February 27th, 2004 06:27 PM
How can i convert english numbers to arabic number zahidpervaiz Classic ASP Databases 0 December 30th, 2003 06:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.