PHP Fundamentals Notes
Server-Side Development: PHP Fundamentals
Introduction
- Server-side development involves technologies that:
- Access data sources.
- Handle security.
- Enable web sites to interact with external services (e.g., payment systems).
- Traditionally, server-side programs generate HTML sent to the browser.
Common Server-Side Technologies
- ASP (Active Server Pages): Microsoft’s initial server-side technology (ASP Classic).
- ASP.NET: Replaced the older ASP technology from Microsoft.
- JSP (Java Server Pages): Uses Java, object-oriented, for large enterprise web systems, integrated into the J2EE environment.
- Node.js (Node): Employs JavaScript on the server side.
- Perl: Excels in text manipulation.
- PHP: Dynamically typed language, embeddable directly within HTML.
- Python: Object-oriented language used for various applications, including web applications.
- Ruby on Rails: Web development framework using the Ruby language.
PHP Language Fundamentals
- PHP is dynamically typed (with optional static typing).
- Offers classes and functions like C++, C#, and Java.
- Syntax for loops, conditionals, and assignment is similar to JavaScript.
PHP Tags and Comments
- PHP code is embedded within HTML files, typically using the
.phpextension. - Code is contained within
<?phpand?>tags. - Code within these tags is interpreted and executed; outside the tags, code is echoed directly to the client.
- PHP files often mix HTML markup and PHP programming logic.
- Comments:
- Multi-line:
/* */ - End-of-line:
//
- Multi-line:
PHP Tag Example
<?php $user = "Randy"; ?>
<!DOCTYPE html>
<html>
<body>
<h1>Welcome <?php echo $user; ?></h1>
<p>The server time is <?php echo "<strong>"; echo date("H:i:s"); echo "</strong>"; ?></p>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome Randy</h1>
<p>The server time is <strong>02:59:09</strong></p>
</body>
</html>
Variables and Data Types
- Dynamically typed: PHP infers the type based on assignment.
- Loosely typed: Variables can be assigned different data types over time.
- Data Types:
- Boolean:
trueorfalse - Integer: Whole numbers
- Float: Decimal numbers
- String: Letters
- Array: Collection of data (discussed later)
- Object: Instances of classes
- Boolean:
PHP Constants
- Constants are defined using the
define()function. - Typically defined at the top of a PHP file.
- Takes two parameters: constant name and value.
- Referenced without the
$symbol.
<?php
// uppercase for constants is a programming convention
define("DATABASE_LOCAL", "localhost");
define("DATABASE_NAME", "ArtStore");
define("DATABASE_USER", "Fred");
define("DATABASE_PASSWD", "F5^7%ad");
// ...
// notice that no $ prefaces constant names
$db = new mysqli(
DATABASE_LOCAL,
DATABASE_NAME,
DATABASE_USER,
DATABASE_PASSWD
);
?>
Writing to Output
- Use
echo()orprintfunction to output to the browser.
echo("hello");
echo "hello"; //alternate version (no parenthesis)
- Concatenation: Use the period
.symbol.
$username = "Ricardo";
echo "Hello " . $username; // Outputs: Hello Ricardo
PHP Quote and Concatenation Approaches
<?php
$firstName = "Pablo";
$lastName = "Picasso";
/*Example one: These two lines are equivalent. Notice that you can reference PHP variables within a string literal defined with double quotes. The resulting output for both lines is: <em>Pablo Picasso</em>*/
echo "<em>" . $firstName . " ". $lastName. "</em>";
echo "<em> $firstName $lastName </em>";
/* Example two: These two lines are also equivalent. Notice that you can use either the single quote symbol or double quote symbol for string literals. */
echo "<h1>";
echo '<h1>';
/* Example three: These two lines are also equivalent. In the second example, the escape character (the backslash) is used to embed a double quote within a string literal defined within double quotes. */
echo '<img src="23.jpg" >';
echo "<img src=\"23.jpg\" >";
?>
More Concatenation Examples
- Examples:
echo "<img src='23.jpg' alt='" $firstName $lastName "'";outputs<img src='23.jpg' alt='Pablo Picasso'>echo "<img src='firstName $lastName' >";outputs<img src='23.jpg' alt='Pablo Picasso' >- `echo "<img src=\