Php-mysql Course

1. Building strings

A string is simply a quoted chunk of characters: letters, numbers, spaces, punctuation, and so forth. Strings can be contained within either single or double quotation marks, and in a HEREDOC construction.
These are all strings:   "Web Courses",   'coursesweb.net',   "1976",   '$5, 78%',   'August, 23, 2011'.
To make a string variable, assign a string value to a valid variable name:

<?php
$name = 'Marius';
$a_date = 'July, 07, 1996';
?>

You must use the same type of quotation mark for the beginning and the end of the string. You can add simple quote in a string delimited by double quotes and vice versa, double quotes when you use single-quote bookends.
If you want to add the same quote mark within a string, it must be escaped (preceding it with a backslash (\)). This keeps PHP from interpreting the second quotation mark as the end of the string.
- Double-quote bookends allow you to escape a whole list of characters:
<?php
$tag = '<p id="idp">Paragraph</p>';         // Double quotes in a string created with simple quotes
$site = "Domain: 'CoursesWeb.net'";         // Simple quotes in a string created with double quotes

$div = "<div id=\"idd\">Content</div> \n";         // Double quotes and "new line (\n)" escaped in a string with same quotes
?>
- The "\n" character combination is seen by PHP as a new line directive when written within a double-quoted string (this does not work the same way with single quotes).

To print out the value of a string, use either echo or print.
To print the value of a variable within a context, use double quotation marks. If you add a variable inside simple quotation marks, will display its name (not its value).
<?php
$name = 'Marius';
echo "Hy $name";        // Hy Marius
echo 'Hy $name';        // Hy $name
?>

• Another way to build a string is to use a construct called HEREDOC. It's used to build longer strings, and therefore makes them more readable to the programmer.
Begin the HEREDOC with three less than (<) signs followed by a name designation for that string. After the string is complete, repeat the name designation on its own line with a terminating semicolon. Here is an example:
<?php
$str = <<< aName
The text content
of this string,
created with HEREDOC construct.
aName;

echo $str;
?>
- PHP has no set limits on how big a string can be.

2. Concatenating Strings

Concatenation is used to join strings. It's performed using the concatenation operator (.).
<?php
$site1 = 'marplo.net';
$site2 = 'coursesweb.net';
echo 'Web sites: '. $site1. ', '. $site2;         // Web sites: marplo.net, https://coursesweb.net
?>
- You see that we used the concatenation operator (.) three times. The second (.) is used to insert a comma and a space character ( ', ' ), to separate the value of the two variables.
If you are concatenating one value to another, you can use the concatenation assignment operator ( .= ).
<?php
$hy = 'Hy ';
$hy .= 'MarPlo';
echo $hy;             // Hy MarPlo
?>

3. Some PHP function for strings

PHP has a lot of useful string-specific functions. Here are just some example, the complete list can be found at the official website: PHP string functions.

1.   trim("string", 'character')

- Strip whitespace (or other characters, specified at 'character') from the beginning and end of a string. Without the second parameter, trim() will strip ordinary white space.

<?php
$str = '   Have a good life ...  ';
var_dump($str);            // string(25) "   Have a good life ...  "

$str = trim($str);
var_dump($str);            // string(20) "Have a good life ..."

?>
- var_dump() displays structured information about its parameter, that includes its type, the number of characters and value.

2.   strlen("string")

- Returns the length of a string.

<?php
$str =  'coursesweb.net/php-mysql/';
echo strlen($str);          // 29
?>

1.   str_word_count("string")

- Counts the number of words inside "string".

<?php
$str = 'Have a good life.';
echo str_word_count($str);                // 4
?>

4.   ucwords("string")

- Returns a string with the first character of each word in "string" capitalized.

<?php
$str = 'hy there, how are you?';
echo ucwords($str);               // Hy There, How Are You?
?>

5.   strtolower("string")

- Returns "string" with all alphabetic characters converted to lowercase.

<?php
$str = 'HY THERE, How are you?';
echo strtolower($str);               // hy there, how are you?
?>

6.   stristr("string", 'needle')

- Return the portion of the string from the beginning of the 'needle' to the end of the "string". If the 'needle' is not found, False is returned. 'needle' and "string" are examined in a case-insensitive manner.

<?php
$str = 'PHP course and tutorials';
$re = stristr($str, 'se');
echo $re;                  // se and tutorials
?>

7.   stripos("string", 'needle')

- Returns the numeric position of the first occurrence of 'needle' in the "string". If the 'needle' is not found, False is returned. 'needle' and "string" are examined in a case-insensitive manner.

<?php
$str = 'PHP course and tutorials';
$re = stripos($str, 'se');
echo $re;                  // 8
?>

8.   strip_tags("string")

- Removes embedded HTML tags from within "string".

<?php
$str = 'Example with <b>html</b> <h6>tags</h6>';
echo strip_tags($str);       // Example with html tags
?>

9.   str_ireplace('search', 'replace' "subjec")

- Replace all occurrences of the 'search' string with the 'replace' string in "subject" (case-insensitive).

<?php
$str = 'PHP course and tutorials';
$re = str_ireplace('course', 'lessons', $str);
echo $re;                  // PHP lessons and tutorials
?>

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
PHP Strings

Last accessed pages

  1. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26370)
  2. Merge Drawing and Object Drawing (4468)
  3. SSEP - Site Search Engine PHP-Ajax (11575)
  4. Align DIVs on the same line (8478)
  5. Deco Tool (2719)

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)