Skip to content

Commit 9c444fe

Browse files
Add files via upload
1 parent 19390a1 commit 9c444fe

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Operators/Conditional-Operators.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Conditional Operators in PHP</title>
7+
</head>
8+
<body>
9+
10+
<?php
11+
12+
# Conditional Operator is used to check condition
13+
# ?: Ternary or Conditional Operator
14+
# If condition is true, first statement is returned else second
15+
16+
echo "<p>Using Conditional Operators in PHP</p>";
17+
18+
# Creating variables
19+
$a=9;
20+
21+
# Using operator ?:
22+
23+
# We cannot use echo in ternary operator
24+
# ($a>8)? echo "a is greater than 8."; : echo "a is greater than 8.";
25+
# This will give the following error
26+
# Parse error: syntax error, unexpected token "echo" in C:\xampp\htdocs\PHP Programming\Operators\Conditional-Operators.php on line 22
27+
28+
# Operator returns argument we can print that
29+
echo "<p>Returning Argument from Operator ?:</p>";
30+
echo ($a>8)?"Variable a is greater than 8.":"Variable a is lesser than 8.";
31+
32+
# Storing return value in a variable
33+
$result=($a>8)?"Variable a is greater than 8.":"Variable a is lesser than 8.";
34+
echo "<p>Storing return value in a variable.</p>";
35+
echo "<p>Result is: ",$result,"</p>";
36+
37+
?>
38+
39+
</body>
40+
</html>

0 commit comments

Comments
 (0)