Professional Documents
Culture Documents
Intro To PHP: Mr. Sunanda Das Assistant Professor, Dept. of CSE, KUET
Intro To PHP: Mr. Sunanda Das Assistant Professor, Dept. of CSE, KUET
Intro To PHP: Mr. Sunanda Das Assistant Professor, Dept. of CSE, KUET
<?php
// PHP code goes here
?>
<!DOCTYPE html>
<html>
<body>
<?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
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";
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$t = "15";
</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");
</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>
</body>
</html>
welcome_get.php
<html>
<body>
</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