Friendly PHP Course – Part #2 Using Constants , Maths,Formating numbers,Strings
Deprecated: Function create_function() is deprecated in /home/vhosts/poweredbygnulinux.com/wp-content/plugins/codecolorer/lib/geshi.php on line 4698
Similar to variables , constants are given a name and a value is stored in them they can’t be changed .
Used when a value is needed several places in the program and doesn’t change.
to see its value .When you echo a constant, you can’t enclose it in quotes .If you do, it echoes the constant name.
Constant names are not preceded by a dollar sign ($).
By convention, constants are given names that are all uppercase.
Maths & Numbers
You indicate arithmetic operations with two numbers and an arithmetic operator , such as:
2 + 3
You can also perform arithmetic operations with variables.
$sum = $nu1 + $nu2;
echo "sum of two numbers = ";
echo " $sum <br />";
The output is
- + – Add and substract
- * / Multiply and Divide
- a%b The reminder of a/b (modulus)
PHP does multiplication and division first, followed by addition and subtraction. If other considerations are equal, PHP goes from left to right.
You can change the order by using parentheses. Its better use them..
formats a number into a dollar amount.
also add commas to separate thousands.
echo " formating number " ;
$f_price = sprintf ("%01.2f", $sum);
The output is
A string is a series of characters. Characters are letters, numbers,and punctuation You tell PHP where the string begins and ends by using double quotes or single quotes.
It won’t work because when PHP sees the ‘ (single quote) it thinks that this is the end of the string.
Escaping the character
The backslash tells PHP that the single quote does not have any special meaning.
When you enclose a string in double quotes, you must also use a backslash in front of any double quotes in the string.
Single quoted & double quoted strings
Single quoted strings are stored literally, with the exception of \
If you enclose a variable in single quotes, PHP uses the literal variable name.
The special characters \n tell PHP to start a new line.
The special characters \t tell PHP to insert a tab.
Double-quoted strings, variables and some special characters are evaluated before the string is stored.
If you enclose a variable in double quotes, PHP uses the value of the variable.
You can concatenate strings by using a dot (.)
$stringconcatenated = $string1.$string2;
use .= to add characters to an existing string
Finally read the summary in a mindmap