PHP Constants

It is just the name for the value. But its value cannot be change in the entire script once it is created. Generally its name starts with either letter or underscore (no other signs)

Syntax:

Name: It specify the name of constant
Value: It specify the value of constant
Case-sensitive: It means whether the value of constant is case sensitive or not. Default it is false.

Example:

Result:

PHP Strings Functions

Searching & Replacing String
Write any some logical PHP program you need to search some text for a particular chunk of string. Like, need to know the domain name of user email id or want to know the current page name from page URL.

In PHP language there are some pre-define function to search particular string from any text.

  1. strpos() and strrpos()
  2. strstr()
  3. substr_count()
  4. i. strpos() and strrpos()

    Output

    Explanation
    strpos() function return the position** of any string value from whole text. The first argument is text value and the second argument is string (which you want to find) value. If the string is found then returns the position of this string and if not found it returns -1.
    In above example, we want to search the gmail position from [email protected] It returns 11 the position value of gmail word.
    But when we search yahoo it returns -1 because yahoo is not present in [email protected]
    strrpos() is similar to strpos() but very minor difference.
    strpos() find the string from the starting position and strrpos() find the string from the ending position of text.
    So above example strpos() return the first dot(.) position and strrpos() return last dot(.) position

    Note: ** String position start with zero. Be careful when you using strpos() to search the first string position in any text its returns zero which equivalent to false in PHP (if you check the code in if condition). That’s why use === or !== operator to avoid those problem.

    ii strstr()

    Output

    Explanation

    strstr() function just check the string is present or not in the text. If present returns the string value along with reaming text and if not present return false.
    So in above example first gmail is found so return gmail.com
    And the second yahoo not found so return Not Found.
    Note: strstr() is case sensitive means if you search Gmail it returns false. If you don’t care about the matching case then use stristr() which is case insensitive.

    iii. substr_count()

    Output

    Explanation
    substr_count() function used to find the number of times of a text appears in the target string.
    In above example, o is present two times so it returns 2.

    In your PHP program sometimes you need to replace wrong text from any string. Like spelling check, Wrong spelling replaces to correct spelling. In this case PHP gives you str_replace() function,

    Example

    Output

    Explanation
    str_replace() function used to replace any striing to another string in the chunk of text. The first argument is which striing you want to replace, the second argument is which string replace the previous string and the third argument is where the to replace is actually happening means the text value.
    In above example, you try to replace the wrong striing spelling to correct spelling. So pass the first argument wrong spelling ‘striing’, the second pass the correct spelling ‘string’ and third the text value ‘using string replace function to correct string spelling’.
    After execution, all ‘striing’ word replace to ‘string’.

    Note: str_replace() is case sensitive means if you want to replace String it returns false. If you don’t care about the matching case then use str_ireplace() which is case insensitive.

    Formatting String

    PHP allows to format strings in many different ways to make easy for people to read or passing to another program.
    There are many PHP function to format strings in specific ways.

    1. printf()
    2. sprintf()
    3. fprintf()
    4. vprintf()

    1. printf()

    Output

    Explanation
    In printf() the first argument is a string, it is called format string. It contains regular text or some format specifiers (like in above example %s). format specifiers start with % symbol.
    The second argument is the value which you want to format particular way.
    In above example 123.45 format into co-responding its float, integer and binary integer value style.
    There are some symbol of different format specifier, like %s for string, %f for float, %d for decimal integer, %b for binary integer, %c for ASCII, %e for scientific notation, %x for hexadecimal integer and %o for octal integer.

    2. sprintf()
    sprintf() is similar to prinf() except that rather than directly outputting the result it returns, it so that you can store it in a variable. This is help to process the result before displaying it or store into database also.
    1. printf()

    3. fprintf() use for writing the result to a file.
    iv.4. vprintf() use for work with the array of values to format instead of an argument list.

PHP Data Types

Variables are those which can store data of various types and these various data types can do many things. There are few variables that PHP support:

  • String
  • Integer
  • Float
  • Boolean
  • Array

1. Strings:
This is sequence of characters which can be put in whether double quotes or single quotes.
Example:

Result:

2 Integer:

Integers are whole numbers in which there would be no decimal. It should have atleast numeric digit, do not include any comma or blank space, can be positive or negative but not any decimal part and it can only be expressed in decimal, hexadecimal and octal decimal .
Example:

Result:

3 float:

This can be presented in decimal format or in exponential form.
Example:

Result:

4 Boolean:

It have two optional with them one is true and another is false.
Syntax:
$a=TRUE;
$B=FALSE;

5 Array:

It can easily store multiple values in single variable.

Example:

Result:

Echo and print Statements

There are two types in PHP by which we can generate output, one is echo and another is print. They are exactly same working but they both have some similarity that echo do not have any return value but print have return value 1. And because of that print can be used in expressions. Echo is very fast as compared to print. It can take multiple arguments but echo can take only one arguments.

1. Echo:
It can be used with or without the parenthesis.
Example:

Result:

PHP!
I’m human being!
This is new way to learn many new things.

2. Print:

It can also be used with or without parenthesis.

Example:

Result:

PHP!
I’m human being!
This is new way to learn many new things.

PHP Variables

Variables are those which is used to store the information. They act like a container. It start with ‘$’ this sign and follow by name of the variable. Variable name should either start with Underscore or some letter but I can never start with a number.

Example:

Result:

My name is PHP
1
1.9

Explanation: $txt always hole the value of some text word then $x and $y will hold the value of some numerical.

Now moving further, PHP has three different types of variable scopes in a program. The three are :

Global

The variable which is declared outside the function then is called Global variable. This variable can be access outside the particular function.

Example:

Result:
This function has Variable:
This function has Variable: 20

Explanation: The variable was declared outside the function will not be call up inside the function, it can only be called at outside the function. While calling the global variable inside the function will give some error.

Local

The variable which is declared inside the function then is called Local variable. This variable can be only access inside the function.

Example:

Result:

Inside Variable is: 20
Outside Variable is:

Explanation: The variable was declared inside the function will not be call up outside the function, it can only be called at inside the function. While calling the local variable outside the function will give some error.

Static

Example:

Result:
10
9
8
7
6

Explanation: In this static type variable, whenever this variable is been called it will for sure give you some output.

PHP Comments

By comment it means that the developers do not want to execute that particular code. It is generally to test that this particular code is running properly or not.

Example:

Result:
PHP stand for

Explanation: If one want to comment a single line then they can use either – “//” or “#” or if one want to comment multiple line then they can use- “/* —code—-*/”
And in the last part you must have seen that particular section has been commented so that part would not be seen by anyone and result would be that part that is not commented.

PHP Syntax and First Example

To start with PHP one should need few things that are as follows:
• Web host with PHP & My SQL support
• First install web server in your computer then install PHP & My SQL.

Syntax of PHP:

Save your file by naming the file of “.php” extension.
The simple starting example for PHP we can use as:
Example:

Result:

PHP stands for Hyper Text preprocessor