Intro To PHP: Mr. Sunanda Das Assistant Professor, Dept. of CSE, KUET

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Intro to PHP

Mr. Sunanda Das


Assistant Professor, dept. of CSE, KUET
PHP ??
• PHP Stands for "Hypertext Preprocessor.“
• PHP is an HTML-embedded Web scripting language.
• Open source, Used to generate dynamic web pages
• Predefined <?php … … ?> tag is used to embed PHP
scripts within HTML
How PHP Works??
Basic PHP Syntax
• A PHP script can be placed anywhere in the document.
• A PHP script starts with <?php and ends with ?>

<?php
// PHP code goes here
?>
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>
PHP Case Sensitivity
• In PHP, NO keywords (e.g. if, else, while, echo, etc.), classes, functions,
and user-defined functions are case-sensitive.
<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>
PHP Comments
• Syntax for single-line comments:
<?php
// This is a single-line comment

# This is also a single-line comment


?>
• Syntax for multiple-line comments:
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
PHP Variables
• In PHP, a variable starts with the $ sign, followed by the name of the
variable:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;

echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
Output Variables
• With PHP, there are two basic ways to get output: echo and print.

<!DOCTYPE html>
<html>
<body>

<?php
$txt = “KUET";
echo "I love $txt!";
Echo “<br>”
echo "I love " . $txt . "!";
print "<h2>PHP is Fun!</h2>";
?>

</body>
</html>
PHP Strings
• strlen() - Return the Length of a String
• str_word_count() - Count Words in a String
• strpos() - Search For a Text Within a String
• str_replace() - Replace Text Within a String
<!DOCTYPE html>
<html>
<body>

<?php
echo strpos("Hello world!", "world");
?>

</body>
</html>

<!DOCTYPE html>
<html>
<body>

<?php
echo str_replace("world", "Dolly", "Hello world!");
?>

</body>
</html>
if...else...elseif Statements
<!DOCTYPE html>
<html>
<body>

<?php
$t = "10";

if ($t < "20") {


echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?php
$t = "15";

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

</body>
</html>
PHP while Loop
<!DOCTYPE html>
<html>
<body>

<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>

</body>
</html>
PHP for Loop
<!DOCTYPE html>
<html>
<body>

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

</body>
</html>
PHP Array
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

</body>
</html>
The Length of an Array

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>

</body>
</html>
PHP Associative Arrays

<!DOCTYPE html>
<html>
<body>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

</body>
</html>
PHP Functions
<!DOCTYPE html>
<html>
<body>

<?php
function writeMsg() {
echo "Hello world!";
}

writeMsg();
?>

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>

</body>
</html>
Classes and Objects
class Fruit {
// Properties
public $name;
public $color;
$apple = new Fruit();
// Methods $apple->set_name('Apple');
function set_name($name) { $apple->set_color('Red');
$this->name = $name; echo "Name: " . $apple->get_name();
} echo "<br>";
function get_name() { echo "Color: " . $apple->get_color();
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
PHP Form Handling

welcome.php
<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>
welcome_get.php
<html>
<body>

Welcome <?php echo $_GET["name"]; ?><br>


Your email address is: <?php echo $_GET["email"]; ?>

</body>
</html>
PHP Form Validation
References
• https://stillat.com/blog/2014/04/02/how-does-php-work-with-the-w
eb-server-and-browser
• https://www.w3schools.com/php/default.asp

You might also like