PHP String Tutorial

PHP programming languages store data as sequences of bits, each of which can contain the value of "0" or "1." However, the way these data are treated by the programming language depends on the data type. For example, the string data type treats this binary data sequence as a segment of text. Strings are very useful in PHP since the language is designed to bring interactive elements to Web pages. Since Web pages contain a lot of text, a PHP programmer must know how to properly use PHP strings.

Instructions

    • 1

      Decide how you will run your PHP code. If you have a PHP server, you can execute code with PHP files. If you do not have access to a PHP server, you can use an online PHP interpreter. Enter the code in this tutorial into either a PHP file or the online PHP interpreter.

    • 2

      Begin your PHP program with the following statement:

      <?php

    • 3

      Write the following code to declare a string variable named "$str0" and assign it some text value. Place the following statement immediately below the statement made in the previous step:

      $str0 = "Hello";

    • 4

      Write the following code to declare a second string variable, this one named "$str1," and assign it some text value, like this:

      $str1 = "World";

    • 5

      Concatenate "$str0" with a single whitespace character, which can be represented by the string " ". String concatenation is the process of appending one string with a second. This is done with the dot operator in PHP, like this:

      $str2 = $str0 . " ";

    • 6

      Concatenate "$str2" with "str1" and store it in "$str3" like this:

      $str3 = $str2 . $str1;

    • 7

      Print out the result by using the "print" function. You can concatenate a special character onto the end of strings to append it with a newline character. This means the entire string is printed on its own line, which can clean up the output of a program and improve its readability.

      print($str3 . "\n");

    • 8

      Try out one of the many fun and interesting string-specific PHP functions available to you. For example, you can randomly shuffle all of the characters in a string and print out the result by adding the following two lines to your code:

      $str4 = str_shuffle($str3);

      print($str4);

    • 9

      Conclude your PHP program with the statement below. Your program is now ready to be tested on your PHP server or the online PHP interpreter.

      ?>

    • 10

      Observe the output of the PHP script. Depending on how the PHP script decides to shuffle the string, the output should look something like this:

      Hello World

      Wleolor Hld

Related Searches:

References

Resources

Comments

Related Ads

Featured