let x = 5;
let y = 3;
function sum() {
console.log(x + y);
}
sum();
//outputs 8 in JavaScript,
$x = 3;
$y = 5;
function sum() {
echo $x + $y;
}
sum();
//outputs error unless declaring variables
function writeMsg() {
console.log("hello world")
}
WRITEMSG();
// outputs reference error
function writeMsg() {
echo "Hello world";
}
WRITEMSG();
// outputs Hello world
$double = 1.345;
printf("%f\n", $double);
to print 2 digits after comma use %.2f or any number
printf("%.2f", $double);
Echo
echo is a statement, which is used to display the output.
echo can be used with or without parentheses.
echo does not return any value.
We can pass multiple strings separated by comma (,) in echo.
echo is faster than print statement.
Print
print is also a statement, used as an alternative to echo at many times to display the output.
print can be used with or without parentheses.
print always returns an integer value, which is 1.
Using print, we cannot pass multiple arguments.
print is slower than echo statement.echo str_replace("World","Peter","Hello world!");
output: Hello Peter!
This function are case-insensitive
echo str_ireplace("WORLD","Peter","Hello good world!");
output: Hello good Peter!
<?php
$a=true ;
$b=1;
// Below condition returns true and prints a and b are equal
if($a==$b){
echo "a and b are equal";
}
else{
echo "a and b are not equal";
}
//Below condition returns false and prints a and b are not equal because $a and $b are of different data types.
if($a===$b){
echo "a and b are equal";
}
else{
echo "a and b are not equal";
}
?>
``==
and ===
operator in PHP ?
In PHP
== is equal operator and returns TRUE
if $a is equal to $b after type juggling and === is Identical operator and return TRUE
if $a is equal to $b, and they are of the same data type
$a=true ;
$b=1;
// Below condition returns true and prints a and b are equal
if($a==$b){
echo "a and b are equal";
}
else{
echo "a and b are not equal";
}
//Below condition returns false and prints a and b are not equal because $a and $b are of different data types.
if($a===$b){
echo "a and b are equal";
}
else{
echo "a and b are not equal";
}