PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn the string methods this is the fifth part , we will take the " Number_Format, Ord, Parse_Str, Print and Printf  " methods.   

  • How to format the number using the number_format method.
  • How to get the ASCII code of the first letter using the ord method.
  • What's the parse_str method and how to use it.
  • How to print a string using print method.
  • How to format the print using printf method.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example) 

Difficulty

  • Basic

Description

1- Number_Format Method 

This function accepts one, two or four parameters (and not three) .

If only the parameter number is given, it will be formatted without a decimal part, but with a comma between each thousand. 

If both number and decimal parameters are provided, number will be formatted with decimal places, one point (".") As a decimal point, and a comma between every thousand. 

With four parameters, number will be formatted with decimal places, decimalpoint as decimal point, and separator as thousands separator. 

It has three parameters  

The first is the number which is the number to dial, if no other parameter is set the number will be composed without decimals and with the comma (,) as the thousands separator. 

The second is the decimal it's optional, specific how many decimals. 

If this parameter is placed, the number will be dialed with a point (.) as a decimal point. 

The third is the decimal point it's optional, specifies which string to use for the decimal point.

The fourth is the separator it's optional, specifies which string to use for the thousands separator, only the first character of the separator is used. 

To use the number_format method we need to pass at least one parameter

number_format($number, decimals, decimalPoint, separator)

I have a number and I want to format it using this method , I will pass the number and I want to print 2 decimals after the decimal point 

$number = 250000;

echo number_format($number, 2);

And we can use the other parameters to sepcify the separator and the decimal point, this is the result

2- Ord Method

The ord function returns the ASCII value of the first character of a string. 

It has the string as parameter and it returns the ascii code of the first character of this string.  

To use the ord method we need to pass the string as parameter

ord($string)

I want to get the ASCII code of the first letter using the ord method

$string = "Utopian";

echo ord($string);

The ord method will return the ASCII code of the letter 'U' and this is the result

3- Parse_Str Method

The parse_str function parses the string as if it were an HTTP request passed through the URL, all the variables it identifies are then created with their respective values. 

To access the QUERY_STRING calling URL you must use the $ _SERVER ['QUERY_STRING'] variable. 

The magic_quotes_gpc configuration affects the display of this function because parse_str uses the same mechanism that PHP uses to propagate $ _GET, $ _POST, and so on. 

It has two parameters  

The first is the string.  

The second is the array , it's optional specifies the name of an array to store the variables, this parameter indicates that the variables will be stored in an array. 

If the second array parameter is provided, the variables will be stored as a table index.  

To use the parse_str method we need to pass the string as parameter

parse_str($string)

I have an HTML form the method is get I want to get the value passed by the input 

<form action="" method="get">

     <input type="text" value="Alexendre" name="username">

     <input type="password" value="Maxim" name="password">

      <input type="submit" value="Connect">

</form>

When we click on " Connect " the URL will be ' www.yoursite.com?username=Alexendre&password=Maxim ' , when we pass it to the parse_str method

parse_str($get, $array);

echo "<pre>";

print_r($array);

 It will divide the string and add the variables to the array and this is the result

4- Print Method

The print print function displays one or more strings, the function is not really a function (it's technically a language structure). 

This means you do not have to use parentheses. It has the string or more strings to send as parameter.  

To use the print method we need to pass the string as parmeter

print($string)

I have a string = ' Utopian ' I want to print it in our page , but the browser will add it without HTML tag

5- Printf Method

The printf function is used to produce a formatted string, in this function, arg1, arg2, arg(n) parameters will be added percent signs (%) in the main string. 

The printf function works "step by step", in this function you must use placeholders, if there are no more% signs in front of the arguments.

It has at least two parameters  

The first is the format specify the string and how to compose the variables in this string. 

The second is the first argument to be inserted at the first% sign in the format string.

The third is also an argument to be inserted at the second% sign ..etc  

To use the printf method we need to pass the format and the argument as parameters

printf(format,arg)

I have a number and I want to print it with different format using the printf method

printf("The number is %d", $number);

printf("<br>The number in binary is %b", $number);

printf("<br>The number is %f", $number);

There is a predefined list to use in the format parameter , %d,%c,%f,%e ..etc and this is the result

Video Tutorial 

Curriculum

Proof of Work Done 

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/string5.php

Sort:  

Hi @alexendre-maxim

Thank you for your contribution to the video-tutorial category.

I can see that you have put effort and made improvement to delivering a good tutorial in your presentation.

Perhaps you can continue to work on the outline and make your tutorials more structured. For example, in your video, around the 15 minutes mark, there is a long silence. But you are actually very active putting in the codes. Sometimes you might need to edit the video and take sections out so that proper explanations are given as people watch the video tutorial.

All this takes time and skill and you are moving along well in this direction.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you @rosatravels , I will remove some parts to become better .

Thank you for your review, @rosatravels! Keep up the good work!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.20
TRX 0.26
JST 0.039
BTC 100104.47
ETH 3619.58
USDT 1.00
SBD 3.10