Search This Blog

Friday, January 8, 2016

PHP numeric functions

PHP numeric functions

Don’t think that PHP’s power is limited to strings only.
The language has over 50 built-in functions for working with numbers, ranging from simple formatting functions to functions for arithmetic, logarithmic, and trigonometric manipulations.
Some of these important functions are
Sr. No Function What it Does
1 ceil() Rounds a number up
2 floor() Rounds a number down
3 abs() Finds the absolute value of anumber
4 pow() Raises one number to the power of another
5 exp() Finds the exponent of a number
6 rand() Generates a random number
7 bindec() Converts a number from binary to decimal
8 decbin() Converts a number from decimal to binary
9 decoct() Converts a number from decimal to octal
10 octdec() Converts a number from octal to decimal
11 dechex() Converts a number from decimal to hexadecimal
12 hexdec() Converts a number from hexadecimal to decimal
13 number_format() Formats number with grouped thousands and decimals
14 printf() Formats a number using a custom specification

Eg i(ceil)
Output
20
In the above example
Initialize variable $num with value=19.7 , output will become 20.
because this function round value up.

Eg ii(floor)
Output
19
in the above example
variable $num = 19.7,and the output will become 19.
Because this function round value down.

Eg iii(abs)
Output
19
In the above example
declare variable ($num) value=19.7 and the output will 19.7
Because abs( ) returns the absolute of given number.

Eg iv(pow)
Output
64
In the above example.
Pass pow( ) function inside echo with value(4,3).
Its multiply (value=4). three times and the result is 64.

Eg v(rand)
Output
55
In the above example
Pass rand( ) function With value from( 10 to 99 ).
it will display any random value lies from 10 to 100.
when we refresh the page on every refresh it show random value like. 22,33 ,44,56 and so on.

Eg vi(bindec)
Output
8
In the above example
bindec( ) function pass inside echo statement with binary value = 1000.
So output will become 8 because bindec( ) function convert binary number into decimal number.

Eg vii(decbin)
Output
1000
In the above example
decbin( ) function Pass inside echo statement with decimal value = 8.
So output will become. 1000

PHP string functions

PHP string functions

PHP has over 75 built-in String manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace.
Some of these important functions are
Sr Function What it Does
1 empty() Tests if a string is empty
2 strlen() Calculates the number of characters in a string
3 strrev() Retrun reverse of a given string
4 str_repeat() Repeats a string no. of times you want
5 substr() Retrieves a section of a string
6 strcmp() Compares two strings
7 str_word_count() Calculates the number of words in a string
8 str_replace() Replaces parts of a string
9 trim() removes leading and trailing whitespaces from a string
10 strtolower() Converts in Lowercases a string
11 strtoupper() Converts in Uppercases a string
12 ucfirst() Converts in uppercase the first character of a string
13 ucwords() Converts in uppercases the first character of every word of a string
14 addslashes() Escapes special characters in a string with backslashes
15 stripslashes() Removes backslashes from a string
16 htmlentities() Encodes HTML within a string
17 htmlspecialchars() Encodes special HTML characters within a sting
18 nl2br() Replaces line breaks in a string with
elements
19 html_entity_decode() Decodes HTML entities within a string
20 htmlspecialchars_decode() Decodes special HTML characters withing a string
21 strip_tags() Removes PHP and HTML code from a string

Here’s example illustrating these operators in action

.
Eg i ( empty( ) )
Output
welcome Rexx
Enter your name
In the above example.
create a textbox and a submit button inside the form.
Pass value inside the textbox and click on button.then the value goes to PHP script page. $_GET is used to accept the value . empty() function is used to check the value which is entered by user.
If type something inside text box, it show a message welcome otherwise if value inside textbox in null (if statement execute) and show a message fill your name.

Eg ii ( strrev( ) )
Output
Your name is palindrome
In the above example
Declare variable $val hold value=”nitin”. Here we use strrev() Function to give reverse of a string.
We pass strrev() function inside If condition. if the reverse string is equal to declared string.
it will print “Your name is palindrome” otherwise “Your name is not palindrome”

Eg iii (str_repeat( ) )
Output
welcome welcome welcome
In the above example
Declare variable $val with value=”welcome”.
use str_repeat( ) function with two argument. first argument declare name of variable, second argument we define number of times print the value.
The output is (welcome welcome welcome) because we pass 3 second argument.

Eg iv ( str_replace( ) )
Output
w@lcom@
In the above example
Declare variable $str with value=”welcome”.
use str_replace( ) function. It accepts three argument: the search term, the replacement term, and the string on which perform replacement.
we have Passed str_replace(“e”,”@”,$str) and
the output is : W@lcom@ because “@” replaced by “e”.

Eg v ( str_word_count( ) )
Output
5
In the above example
Use str_word_count( ) function is used to count the number of word in a string.
declare variable $str value=”hello user how are you”.
pass this function inside echo so the output is :5 (count words separated by space)

Eg vi ( strcmp( ) )
Output
1
In the above example
declare two variable $str value=(“hello”)
$str1 with value=(“HELLO”)
Now compare two string using strcmp( ) function.
display the output i.e 1 because both variable doesn’t contain same value(one is in lowercase while other in uppercase).

Eg vii(strlen( ))
Output
name must be greater than 5
Enter your name

Eg viii ( strpos( ) )
Output
2

Eg ix ( nl2br( ) )
Output
hello
user
how
are
you

Eg x( substr( ) )
Output
php
In the above example
substr( ) function is used to slice string into smaller section.
it accepts three argument: (given string ,the position at which start slicing , and the number of character return from the starting position ).
$str hold a string value=”welcome to the world of php”
now pass substr($str,24,3) function inside echo and the output will become: php

Foreach Loop in PHP

Foreach Loop in PHP

The foreach Loop is used to display the value of array.
You can define two parameter inside foreach separated through “as” keyword.
First parameter must be existing array name which elements or key you want to display.
At the Position of 2nd parameter, could define two variable: One for key(index)
and another for value.
if you define only one variable at the position of 2nd parameter it contain arrays value (By default display array value).
Syntax
For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) – so on the next loop iteration, you’ll be looking at the next array value.
The following example demonstrates a loop that will print the values of the given array.
Output
alex
simon
ravi
In the above example
declare an array variable($person) hold the elements of array. Here we want to print all element of an array without passing index value.
We used foreach( ) loop. Passing Variable name ($person as $val).
it means $val collect all elements of an array. Pass $val with echo statement it show all element as output.

Define colors name and their index

Output
r–red
g–green
b–black
w–white
In the above example
$color variable hold the values (“red”,”green”,”black”,”white”) on index(“r”, “g”, “b”, “w” ).
if we want do display all values with their index then used foreach( ) loop.
Inside foreach( ) we have passed three arguments array name, index($key) and value($val) separated by “as”.
Now call the variable $val to display array values and $key for index.

Find the Sum of given array

Output
Sum of given array = 75
In the above example
Declare variable $array hold the elements of an array, variable $sum hold value=0,
pass( $array as $x) inside foreach( ) loop.
It call the values of array one by one and make sum ($sum=$sum+$x) till ends of array.
at last pass $sum with echo statement to display the sum of given array, output will become.