Php-mysql Course

A PHP script can be very simple or very complex. However, even writing a complex PHP script is relatively simple, requiring only an regular text editor. PHP code is plain text and can be created in any text editor, such as Notepad on Windows or TextEdit on Mac OS X. Anyway, it's indicated and more efficiently to use a dedicated script editor like Notepad++ or Eclipse.
PHP is a programming language that needs a server to run it. First, you must have installed a server, for example Apache and the PHP module. They are free, but for beginners is better to install an all-in-one packages free aplication, like WampServer (www.wampserver.com/en/) or XAMPP, that already has Apache, PHP, MySQL and phpMyAdmin included and configured.
You can get XAMPP from the page: Download XAMPP.

- Once you have installed XAMPP (WampServes or EasyPHP) aplication, you need to create your PHP files in a location where the web server can process them. Normally, this means that the files should be in the serve`s directory root or a subfolder of the document root. The default location of the document root for these aplications is as follows:

- Because PHP scripts need to be parsed by the server, you absolutely must access them via the URL. The URL for the web serve`s directory root in your local computer is http://localhost/.

1. Writing the structure of a PHP script

Every PHP script contains two special lines (or PHP tags) that indicate to PHP server that the text contained between the two tags must be parsed as PHP instructions. Between these two lines will be written the PHP code.
To start writing a PHP script, open your text editor and insert these two lines:
<?php
?>
- This is a start code and displays nothing.
You can save your script (with ANSI or UTF-8 format) in a text file with the ".php" extension, in the "htdocs" (for XAMPP) or "www" (for WampServer) folder, with a name that respect the following rules:

Any text outside of the PHP tags is immediately sent to the Web browser as regular HTML text.

2. Display output data in a Web browser

PHP scripts execute three basic operations:
Let`s write a simple PHP script that displays a text in the browser window. Open your text editor and add the fallowing code:
<?php
echo 'The text that appears in your browser.';
?>
Save the file with a name with the ".php" extension (for example "test1.php"), in the proper directory of your Web server, "xamp/htdocs/" (or "wamp/www/").
To see the result, access in the browser the fallowing address: http://localhost/test1.php
If everything is made correctly, the browser will displays "The text that appears in your browser.".
- Notice that the instruction begins with a word "echo" and ends with a semicolon (;).
Each code line in PHP must end with a semicolon (;), it is used to distinguish one set of instructions from another.
The "echo" statement sends output data to be displayed in the browser (There are two basic statements to output text with PHP: echo and print.).
Simple (or double) quotes are used to delimit strings (a text expression), in our case 'The text that appears in your browser.'.

• PHP is an HTML-embedded scripting language. This means that you can combine (intermingle) PHP and HTML code within the same file. Also, PHP can generate (X)HTML code.
For example, here is a PHP script that outputs a HTML code and it's placed inside a HTML document, in the BODY section.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test 2 PHP</title>
</head>
<body>
<?php
echo "<h4>Example of a PHP scripts included in HTML</h4>";
?>
</body>
</html>
- If you save this code in a ".php" file (for example, "test_html.php"), in the server's directory root, and access it in your browser (http://localhost/test_html.php), you'll see in the Web browser the fallowing result:

Example of a PHP scripts included in HTML


You can embed multiple blocks of PHP code within a single HTML page, anywhere in the documen. For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
<?php
echo 'Page title';
?>
</title>
</head>
<body>
<?php
echo '<div>Free PHP-MySQL course: https://coursesweb.net/php-mysql/</div>';
?>
</body>
</html>

3. Writing Comments

A secondary but still important aspect to any programming is documenting your code.
In PHP, you can use // to make a single-line comment or /* and */ for multiline comment block.
  - Example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Comment PHP</title>
</head>
<body>
<?php
// This is an one-line comment

echo '<h3>coursesweb.net</h3>';

/*
This is
a multiline
comment block
*/
?>
</body>
</html>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Writing PHP scripts

Last accessed pages

  1. SSEP - Site Search Engine PHP-Ajax (11575)
  2. Align DIVs on the same line (8478)
  3. Deco Tool (2719)
  4. jQuery Drag and Drop Rows between two similar Tables (12974)
  5. Creating Custom Colors (2363)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (526)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)