some thing practical
<?php
define('val___nul', "");
define('val___nul5', "fillup feild!_please");
?>
<?php
$a=val___nul!=val___nul5;
if($a=== true){ echo val___nul5;}
else {echo "its not ok";}
?>
<br>
First, you specify the operator <<<, followed by an IDENTIFIER, and then a newline. Next, you put a string value. Finally, you use the same
IDENTIFIER to close the string.
The following example defines a string using heredoc syntax:
<?php
$s = <<<EOT
I'm a string that is using
heredoc syntax. I can contain
single quotation marks (') and double quotation
mark(") without using escape character
EOT;
echo $s;?>
Notice that PHP replaces the variables by their values in a string defined using heredoc syntax.
The nowdoc syntax is similar to the heredoc syntax except the identifier in nowdoc syntax is enclosed in single quotation marks.
<?php
$s = <<<'EOT'
I'm a string that is using
nowdoc syntax. I can contain
single quotation marks (') and double quotation
mark(") without using escape character
EOT;
echo $s;?>
In addition, PHP does not parse and replace variables by their values in a string defined using nowdoc syntax.
Accessing characters in a string
To access a character of a string at a particular position, you use the following syntax:
$s[index]
<br>
<?php
$str = 'this is a PHP string';
$str[0] = 'T';
echo $str;?>
<br>
Getting the length of a string
To get the number of characters in a string, you use a built-in function strlen(). It accepts a string as a parameter and returns the number of
characters in that string. Let’s take a look a the following example of using the strlen() function:
<?php
$str = 'this is a PHP string';
echo strlen($str);?>
<br>
If you want to count the number of words in a string, you use the str_word_count() function instead.
<br>
<?php
$str = 'this is a PHP string';
echo str_word_count($str);
// 5
?>
<br>
Comparing strings
PHP provides two built-in functions strcmp() and strcasecmp() that allow you to compare two strings. Both functions accept two string arguments and
return an integer number:
If two strings are equal, it returns 0.
If the first string is greater than the second string it returns a number that is greater than 0 .
If the first string is less than the second string, it returns a number that is less than 0.
The only difference between strcmp() and strcasecmp() functions is that the strcasecmp() function compares strings in case-insensitive manner
whereas the strcmp() function does not.
<br>
<?php
$str1 = 'PHP String';
$str2 = 'php string';
$result = strcasecmp($str1,$str2);
if($result == 0){
echo '$str1 and $str2 are equal';
}else if($result > 0){
echo '$str1 is greater than $str2';
}else{
echo '$str1 is less than $str2';
}?>
<br>
If you replace the strcasecmp() function by strcmp() function in the code snippet above, the output will be different.
Concatenating strings
PHP allows you to concatenate strings using period ( .) operator. If you concatenate a string with a variable that is not a string type, the result
is a string because PHP automatically converts the non-string variable into a string variable before concatenating.
The following example demonstrates strings concatenation:
<br>
<?php
$str = 'This is' . ' a string';
echo $str . '<br/>';
$age = 22;
$s1 = 'years old';
$s2 = 'Are you ' . $age . ' ' . $s1 . '?';
echo $s2;
?>
<br>
amzn_assoc_placement = "adunit0";
amzn_assoc_tracking_id = "sellingon-20";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_default_search_phrase = "Beauty";
amzn_assoc_default_category = "All";
amzn_assoc_linkid = "8a5aeb9d02c991348d0d037e1d535d71";
amzn_assoc_design = "in_content";
amzn_assoc_title = "Search your favorites but";
Thursday, November 29, 2018
Subscribe to:
Post Comments (Atom)
form validation
function formsubmit ( ) { var empname = document .getElementById ( 'emp_name' ). value ; var email = document .getElem...
-
PHP Compare Objects Summary : in this tutorial, you will learn how to compare objects in PHP using comparison operator ( == ) and ident...
-
<!DOCTYPE html> <html> <title>basic encruption example</title> <head> <?php // DEFINE our cipher ...
No comments:
Post a Comment