PHP Tutorial #01 String (Display Functions, String Information and Position of a String )

in #utopian-io5 years ago

Image Source

Repository

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

What Will I Learn?

  • You will learn Display functions.
  • You will learn String Information.
  • You will learn Position of a String.

Requirements

  • Text Editor.
  • Server support PHP.
  • Browser.

Difficulty

  • Basic

Tutorial Contents

Almost all data in all web scripts are textual data, for that it is necessary to understand these textual data, these characteristics, these functions in the languages of programming to handle them easily.

PHP is a language that manipulates many textual data and that offers services and functions for the developer to process and to manipulate this data, then it is a solution for the programming of low level and the meaning of use the word " low level " is to set an example to display a string of characters with this level will be a procedure created by the developer to browse the string character by character, on the contrary in high-level languages like PHP, there is plain functions that directly display the string.

And there are plenty of other functions, such as shape transformation functions, validation functions, security functions etc. The goal of this tutorial is to learn 3 concepts using predefined functions.

1- Display functions

To display a string there are two types :

1-a Simple displays

The most important functions for this type of display are: "echo, print", these functions are more known in the world, even if the developer does not know how to program with PHP he is surely knows this term or the names of these functions , these functions allow to display any type of data passed as a parameter, the parentheses for these functions are useless.

echo "Hello Steemit"; // print the string
echo ("Hello Utopian"); // print the string
print(10); // print the number 10
print $var; // print the value of the variable $var

The functions accept any type of data, character string, integer, floats..etc, also for the function "echo" it can display a set of values concatenated by points, but just when it is used without parentheses.

echo "Hello Steemit". 10 . "Hello Utopian", $var ; // print the string 

1-b Displays with masks

instead of using values concatenated by commas, PHP offers a very good treatment that is the generic display or the display parameterized, the parameters are predefined characters precede by percentages signs, and there are several characteres that depends on the type of the developer wants to display it.

The types of values are the defined types in PHP for example "Strings, Floats, Integers ..etc", here is an image with the characters and the description for each one, the source is the official site of PHP.

To apply the previous example with the "printf" function, the characters that will be used are: "d, s, and f", assuming the variable "var" is a floating number.

$wlcm1 = "Hello Steemit";  // define the variable $wlcm1
$wlcm2 = "Hello Utopian"; // define the variable $wlcm2
$number = 10; // define the variable $number
$var = 10.1; // define the variable $var
$all = "%s and %s there is %d persons and %f dollars"; // use the special characters to form the string
printf($all,$wlcm1,wlcm2,$number,$var) ; // using print with format to form the string

So the result will be "Hello Steemit and Hello Utopian there is 10 persons and 10.1 dollars ".

Let's use an example to apply some of these characters

$number = 100;
$string = "Hello";
$display = "Hello the intger number is %d and the floating number is %f, the ASCII code is %c, the binary code is %b, the hexadecimal number is %x"; // example with 5 special characters to see the result of the number with different formats
printf($display,$string,$number, $number, $number, $number, $number, );

The result is 

So this is just an example, you can almost understand how to form the display using these special characters with the percentage sign.

Minimum size of a data

To define in a context a minimum data size, there are several ways to do it, for example by using the same function but by adding spaces for example between the chains or numbers of digits, the essential that this presentation 'spaces , digits ..etc ' will be in your app view.

printf('%-2s Steemit', 'Hello') ; // will display Hello  Steemit (2 spaces)
printf('%02d', 5) ; // will display 05 because the number of digits is 2 

To apply the ideas of spaces and numbers the preceding code will make two spaces between the word "Hello" and the word "Steemit", and also the display of numbers where the result is "05" at place of 5.

As the specification of the minimum size of an integer also there is the specification of the floating number, to limit the number of digits after the comma and the size of all the number it is by the use of this code.

printf('%05.2f', 5.0) ; // will display 05.00 

The result that will be displayed in the application is: "05.00".

2- String Information

2-a Access to a specific character

The string is an array of characters, each box of this array contains a character, and since it is an array whereas each box indexed by an index, to recover a character it is necessary first to mention its position by using the braces.

$string = "Hello Steemit"; // define string variable
echo $string[6]; //will display "S"

The string starts from 0, so the position of the first character is ' 0 ', the second character is ' 1 ' and so on, and for that I have set the position value to 6 to get the character ' S '.

2-b Getting the ASCII value of a character

Sometimes and especially in all the sites conversion sites from string to ASCII code, they use two important functions that are the 'ord' for a character string converter to an ASCII and the 'chr' for the opposite, so we conclude that to make a converter input from String to ASCII is very easy by using the 'ord' and 'chr' functions.

echo "The character K in ASCII Representation is " . ord('K') ."
"; // print the sentence concatenated with the result of the function ' ord '
echo "The value of the 75 ASCII code is " . chr(75); // print the sentence concatenated with the result of the function ' chr'

The example is for the character ' K ', there is plenty of tutorials on ASCII on the internet, here is a table that represents the characters and the representation of each character in ASCII (Source).

To confirm, we will use a 'loop' to display the ASCII codes of the characters 'A, B, C, D, E, F, G, H, I, J, K, M'

2-b The size of a string

As the string is a character array then it is able to recover the size using the function 'sterlen' that accepts the string as parameter.

The function also calculates spaces, signs, white characters, etc.

for ($c=65 ; $c <= 77 ; $c++){
   echo chr($c); // print the character after the convert
}

The problem it poses is the encoding, the encoding system of ' iso ' for example it is not the same as the ' UTF-8 ' in the storage of the characters in the memory, for that there are other functions that are Useful for this problem, you can search around that and understand it well.

There is another function that returns the size of a string which is ' str_word_count() ', and it accepts the string as parameter.

$string  = "Hello Steemit";
$array = str_word_count($string); // the function accepts the string as parameter
echo "<pre>";
print_r($array);
echo "<pre>"

2-c List words from a string

To complete the previous example, the function " str_word_count() " actually accepts two parameters, the first is the string of characters, the second is an attribute that will return an array of your elements, here is the example

$string  = "Hello Steemit"; // string variable
$array = str_word_count($string,1); // the function accepts the string and the second value which will return the array
echo "";
"; // using the pre code to orginize the display
print_r($array); // print the array
echo "

The string to get is " Hello Steemit ", the value of the second attribute is 1 so the index will be the order of words and it will start from 0 and return two elements in the array.

3- Position of a String

To retrieve the position of a character or substring in a string, the " strpos() " function is used, this function accepts 3 parameters:

The first parameter is the string that the function will fetch in.

The second parameter is the substring that the function will take to search in the string and it can be character or substring.

The third is the position to start searching.

echo strpos($string,"S"); // will return the position of this character

This function is very efficient to return the position of the value and it is identical, so the search can be performed from left to right or vice versa.

In the previous example, the searched character is "S", the result or position of this character is 6.

3-a Presence of certain characters

To search for a character in a string there is another method that is by the function 'strcspn', this function will test character by character until the value passed as a parameter, to test the statement ' if ' is used to compare the return value with the function' strcspn 'and' strlen ', this is an example

$String = "Hello Steemit"; // define the string variable
$character = "i" ; // define the character ' i '
if( strcspn($String, $character) != strlen($String) ) { // test if the result of strcspn equals to the length of the string
echo "The character 'i' is found "; // print the character 'i' is found
} else {
echo "The character 'i' is not found" ;// print the character 'i' is not found  
}

The string " Hello Steemit " and the character " i " the ' strcspn ' function will return ' 11 ' it means there is 11 characters then the character ' 12 ' is the letter ' i ', so when the length of the string not eqauls to the returned value of the ' strcspn ' function means that this character exists.

Curriculum

 The first tutorial.

Proof of Work Done

 https://github.com/alex-harry/phpTutorial/blob/master/php1.php 

Sort:  

Thank you for your contribution.

  • Your tutorial contains some contradictory statements, especially in the introduction. You define PHP as low level, and then as high level. Your English statements can be very confusing.
  • The quality of images you used is low, try to use higher and better quality images
  • When you described strpos, you mentioned this requiring 3 params, your example used only 2.
  • The tutorial essentially covers very basic aspects of PHP which mostly found everywhere, i would recommend you focus on more useful and innovative aspects in tutorials.

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 for your review, @mcfarhat! Keep up the good work!

Hi @alex-harry!

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

Congratulations @alex-harry! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You distributed more than 10 upvotes. Your next target is to reach 50 upvotes.
You received more than 100 as payout for your posts. Your next target is to reach a total payout of 250

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!
SteemitBoard - Witness Update
Do not miss the coming Rocky Mountain Steem Meetup and get a new community badge!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Hey, @alex-harry!

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.31
TRX 0.11
JST 0.035
BTC 66739.07
ETH 3228.97
USDT 1.00
SBD 4.23