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'; ?>
<?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).
<?php $name = 'Marius'; echo "Hy $name"; // Hy Marius echo 'Hy $name'; // Hy $name ?>
<?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.
<?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.
<?php $hy = 'Hy '; $hy .= 'MarPlo'; echo $hy; // Hy MarPlo ?>
- 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.
- Returns the length of a string.
<?php $str = 'coursesweb.net/php-mysql/'; echo strlen($str); // 29 ?>
- Counts the number of words inside "string".
<?php $str = 'Have a good life.'; echo str_word_count($str); // 4 ?>
- 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? ?>
- Returns "string" with all alphabetic characters converted to lowercase.
<?php $str = 'HY THERE, How are you?'; echo strtolower($str); // hy there, how are you? ?>
- 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 ?>
- 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 ?>
- 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 ?>
- 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 ?>
<input type="checkbox" name="a_name" value="value" checked="checked" />
#id { background:url("path_to_image.png"); background-size:contain; background-repeat:no-repeat; }
var rest8_7 = 8 % 7; alert(rest8_7);
$nr = ceil(3.5); echo $nr; // 4