Chapter 2. Working with Text and Numbers

4th semester

Unit 1 Unit 3 Unit 4 Unit 5 Unit 6 Unit 7 Unit 8 Unit 9
Unit 10

Data Types

Data Types defines the type of data a variable can store. PHP allows eight different types of data types.

  • String :- A string is a sequence of characters, like “Hello world!”.
  • Integer :- An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
  • Float (floating point numbers – also called double) :-A float (floating point number) is a number with a decimal point or a number in exponential form.
  • Boolean :-  have only two possible values either true or false.
  • Array :- An array stores multiple values in one single variable.

 

Variables in PHP

Variables are used for storing values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;
Let’s try creating a variable containing a string, and a variable containing a number:

 

PHP is a Loosely Typed Language

In PHP, a variable does not need to be declared before adding a value to it.In the example above, you see that you do not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value.In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.In PHP, the variable is declared automatically when you use it.
 Rules for Variables

  •  A variable name must start with a letter or an underscore “_”
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0- 9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
  • A string variable is used to store and manipulate

 

PHP Operators

Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Arithmetic operators

  • Image result for PHP Arithmetic Operators

Comparison Operators

Image result for PHPComparison Operators

  • Assignment operators

Image result for PHP Assignment operators

Logical Operators

Image result for PHP Logical Operators
 

https://www.youtube.com/watch?v=mR6Kz2iIRRs

 Strings

Strings can be seen as a stream of characters. For example, ‘G’ is a character and ‘BIM STUDY NOTES’ is a string. We have learned about basics of string data type in PHP in PHP | Data types and Variables.

Built-in String functions

Built-in functions in PHP are some existing library functions which can be used directly in our programs making an appropriate call to them. Below are some important built-in string functions that we use in our daily and regular programs:
1. strlen() function: This function is used to find the length of a string. This function accepts the string as argument and return the length or number of characters in the string.Example:-

output:- 12
2. strpos() function: This function takes two string arguments and if the second string is present in the first one, it will return the starting position of the string otherwise returns FALSE. Example:The strpos() function is used to search for character within a
Example:-

output:- 6
Note :- The position of the string “world” in our string is position 6. The reason that it is 6 (and not 7), is that the first position in the string is 0, and not
3. others:
a )strrev() function: This function is used to reverse a string. 

Output

!dlrow olleH

b)str_replace() function: str_replace function is used to replaces some characters with some other characters in a string. In below example we replace world with Faiz.

c)trim() function: This function allows us to remove whitespaces or strings from both sides of a string. Example:

Creating Strings

There are two ways of creating strings in PHP:
Single-quote strings: This type of strings does not processes special characters inside quotes.

Double-quote strings : Unlike single-quote strings, double-quote strings in PHP is capable of processing special characters.

In the above program we can see that the double-quote strings is processing the special characters according the their properties. The ‘\n’ character is not printed and is considered as a new-line. Also instead of the variable name $site, “BIM STUDY NOTES” is printed. PHP treats everything inside double quotes(” “) as Strings
Note:- Strings within single quote ignores the special characters but double-quoted strings recognize the special characters and treat them differently.