3.1 PHP echo and print
-
-
-
-
- Outputs one or more strings.
- Example:
<?php
echo("Hello World!");
?>
-
-
-
- print:
-
- Outputs a string and returns a value (1 for success).
- Example:
<?php
print “Hello, PHP!”;
?>
-
3.2 Comments in PHP
-
- Single-line comments:
<?php
// This is a single-line comment
?>
// This is a single-line comment
?>
-
- Multi-line comments:
<?php
/*
This is a multi-line comment.
Used for detailed explanations.
*/
?>
/*
This is a multi-line comment.
Used for detailed explanations.
*/
?>
3.3 Variables and Data Types
- Variables:
-
- Start with
$
and are case-sensitive. - Example:
- Start with
<?php
$name = “John”;
echo $name; // Outputs: John
?> -
- Data Types:
-
- String: Text data. Example:
"Hello"
- Integer: Whole numbers. Example:
42
- Float: Decimal numbers. Example:
3.14
- Boolean:
true
orfalse
. - Array: Collection of values. Example:
- String: Text data. Example:
<?php
$colors = array(“Red”, “Green”, “Blue”);
?>- Object: Instances of classes.
- NULL: Represents no value.
- Resource: Special data type for file and database handlers.
-
Example: Combining Concepts
<?php
“;
echo “Age: $age
“;
echo “Is a student: ” . ($is_student ? “Yes” : “No”);
?>
?>
“;
echo “Age: $age
“;
echo “Is a student: ” . ($is_student ? “Yes” : “No”);
?>
?>
Activities and Exercises
- Quiz: Multiple-choice questions on PHP basics and features.
- Assignment: Create a PHP file that displays a short introduction about yourself, including your name, age, and hobbies.
- Hands-On:
- Create a script that calculates the area of a rectangle.
- Write a script that outputs the current date and time using the
date()
function.
Leave a Reply