Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

My PHP

Creating our first PHP file


Create a new file with the name index.php. Yes, this time, it is PHP. To get the VSCode
BoilerPlate Tap ! and then press enter. Now create a div container in the body and type "This
is my first PHP website." All right, now go to your browser type localhost/cwh. You did it.
Congratulations on creating your first PHP website.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>PHP Tutorial</title>
</head>
<body>
<div class="container">
This is my first php website
</div>
</body>
</html>

To Print In php

<?php
echo "Hello world and this is printed using PHP";
?>

echo is, it outputs one or more strings

 Comment Line in php

Compiled By Yash Sudhir Patil


 // Single line comment
 /*
 This
 is
 a
 multi
 line
 comment
 */

 Variable In php

Variables are nothing more than a container for holding the values in it. In PHP,
you can create a variable by including the “ $ “ before declaring it.

$variable1 = 5;
$variable2 = 2;
echo $variable1;
echo $variable2;

PHP is a dynamically typed language, meaning here, you don’t have to declare the
data types. PHP is not case sensitive that means classes, functions, and user-
defined functions are not case sensitive

 Operators In pHp

Operators are used to performing operations on variables. Operators are symbols that tell the
PHP processor to perform certain actions. For example, the addition (+) symbol is an operator
that tells PHP to add two variables or values, while the greater-than (>) symbol is an operator
that tells PHP to compare two values.

Compiled By Yash Sudhir Patil


PHP Arithmetic Operators
Operator Description Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y.
* Multiplication $x * $y Product of $x and $y.
/ Division $x / $y Quotient of $x and $y

 Arithmetic Operators

 // Arithmetic Operators
 echo "<br>";
 echo "The value of varible1 + variable2 is ";
 echo $variable1 + $variable2;
 echo "<br>";
 echo "The value of varible1 - variable2 is ";
 echo $variable1 - $variable2;
 echo "<br>";
 echo "The value of varible1 * variable2 is ";
 echo $variable1 * $variable2;
 echo "<br>";
 echo "The value of varible1 / variable2 is ";
 echo $variable1 / $variable2;
 echo "<br>";

 Assigment Operators
PHP Assignment Operators
These are used to assign a value from one variable to another
Operator Description Example Result
= Assign $x = $y $x = $y
+= Add and assign $x += $y $x = $x + $y
-= Subtract and assign $x -= $y $x = $x - $y
*= Multiply and assign $x *= $y $x = $x * $y
/= Divide and assign quotient $x /= $y $x = $x / $y
The following example will show you these arithmetic operators in action:

Compiled By Yash Sudhir Patil


x=y

The left operand gets set to the value of the expression on the right

x = y

Copy

x += y

Addition

x = x + y

Copy

x -= y

Subtraction

x = x - y

Copy

x *= y

Multiplication

x = x * y

Copy

x /= y

Division

x = x / y

Copy

x %= y

Modulus

x = x % y

Compiled By Yash Sudhir Patil


Here, the echo “<br>” tag is for the next line, line break .HTML tags can be
included here using the echo.

PHP Comparison Operators


Used to compare two values in Boolean fashion.
operator Name Example Result
== identical $x == $y True if $x is equal to $y,
< Less than $x < $y True if $x is less than $y
> Greater than $x > $y True if $x is greater than $y
Greater than or True if $x is greater than or
>= $x >= $y
equal to equal to $y
True if $x is less than or
<= Less than or equal to $x <= $y
equal to $y

// Comparison Operators
// echo "<h1> Comparison Operators </h1>";
echo "The value of 1==4 is ";

Compiled By Yash Sudhir Patil


echo var_dump(1==4);
echo "<br>";

echo "The value of 1!=4 is ";


echo var_dump(1!=4);
echo "<br>";

echo "The value of 1>=4 is ";


echo var_dump(1>=4);
echo "<br>";

echo "The value of 1<=4 is ";


echo var_dump(1<=4);
echo "<br>";

Copy
Comparison can be used in if-else statements.
Here var_dump() holds type and value of the variable(s).

PHP Incrementing and Decrementing Operators

Pre-increment (++i) − Before assigning the value to the variable, the value is
incremented by one.
Post-increment (i++) − After assigning the value to the variable, the value is
incremented.
Operator Name Effect
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one

// Increment/Decrement Operators
// echo $variable1++;
// echo $variable1--;
// echo ++$variable1;
echo --$variable1;

Compiled By Yash Sudhir Patil


echo "<br>";
echo $variable1;

PHP Logical Operators


Operator Name Example Result
And And $x and $y True if both $x and $y are true
Or Or $x or $y True if either $x or $y is true
Xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true

// Logical Operator
// and (&&)
// or (||)
// xor
// !

// $myVar = (true and true);


// $myVar = (false and true);
// $myVar = (false and false);
// $myVar = (true and false);
// $myVar = (true or false);

// $myVar = (true xor true);


// $myVar = (false and true);
// $myVar = (false xor false);
$myVar = (true and false);
echo "<br>";
echo var_dump($myVar);

Compiled By Yash Sudhir Patil


Compiled By Yash Sudhir Patil

You might also like