They are sequences of characters, like "PHP supports string operations".
NOTE: Built-in string functions is given in function reference
PHP String Functions
Following are valid examples of string
$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
|
Singly quoted strings are treated almost literally, whereas doubly
quoted strings replace variables with their values as well as specially
interpreting certain character sequences.
<?
$variable = "name";
$literally = 'My $variable will not print!\\n';
print($literally);
$literally = "My $variable will print!\\n";
print($literally);
?>
|
This will produce following result:
My $variable will not print!\n
My name will print
|
There are no artificial limits on string length - within the bounds
of available memory, you ought to be able to make arbitrarily long
strings.
Strings that are delimited by double quotes (as in "this") are preprocessed in both the following two ways by PHP:
- Certain character sequences beginning with backslash (\) are replaced with special characters
- Variable names (starting with $) are replaced with string representations of their values.
The escape-sequence replacements are:
- \n is replaced by the newline character
- \r is replaced by the carriage-return character
- \t is replaced by the tab character
- \$ is replaced by the dollar sign itself ($)
- \" is replaced by a single double-quote (")
- \\ is replaced by a single backslash (\)
String Concatenation Operator
To concatenate two string variables together, use the dot (.) operator:
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
?>
|
This will produce following result:
If we look at the code above you see that we used the concatenation
operator two times. This is because we had to insert a third string.
Between the two string variables we added a string with a single character, an empty space, to separate the two variables.
Using the strlen() function
The strlen() function is used to find the length of a string.
Let's find the length of our string "Hello world!":
<?php
echo strlen("Hello world!");
?>
|
This will produce following result:
The length of a string is often used in loops or other functions,
when it is important to know when the string ends. (i.e. in a loop, we
would want to stop the loop after the last character in the string)
Using the strpos() function
The strpos() function is used to search for a string or character within a string.
If a match is found in the string, this function will return the
position of the first match. If no match is found, it will return FALSE.
Let's see if we can find the string "world" in our string:
<?php
echo strpos("Hello world!","world");
?>
|
This will produce following result:
As you see the position of the string "world" in our string is
position 6. The reason that it is 6, and not 7, is that the first
position in the string is 0, and not 1.
This session demonstrates how PHP can provide dynamic content
according to browser type, randomly generated numbers or User Input. It
also demonstrated how the client borwser can be redirected.
Identifying Browser & Platform
PHP creates some useful
environment variables that can be seen in the phpinfo.php page that was used to setup the PHP environment.
One of the environemnt variables set by PHP is
HTTP_USER_AGENT which identifies the user's browser and operating system.
PHP provides a function getenv() to access the value of all the
environment variables. The information contained in the HTTP_USER_AGENT
environment variable can be used to create dynamic content appropriate
to the borwser.
Following example demonstrates how you can identify a client borwser and operating system.
NOTE: The function preg_match()is discussed in
PHP Regular expression session.
<html>
<body>
<?php
$viewer = getenv( "HTTP_USER_AGENT" );
$browser = "An unidentified browser";
if( preg_match( "/MSIE/i", "$viewer" ) )
{
$browser = "Internet Explorer";
}
else if( preg_match( "/Netscape/i", "$viewer" ) )
{
$browser = "Netscape";
}
else if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}
$platform = "An unidentified OS!";
if( preg_match( "/Windows/i", "$viewer" ) )
{
$platform = "Windows!";
}
else if ( preg_match( "/Linux/i", "$viewer" ) )
{
$platform = "Linux!";
}
echo("You are using $browser on $platform");
?>
</body>
</html>
|
This is producing following result on my machine. This result may be different for your computer depnding on what you are using.
You are using Mozilla! on Windows!
|
Display Images Randomly
The PHP
rand() function is used to generate a random number.i
This function can generate numbers with-in a given range. The random
number generator should be seeded to prevent a regular pattern of
numbers being generated. This is achieved using the
srand() function that specifiies the seed number as its argument.
Following example demonstrates how you can display different image each time out of four images:
<html>
<body>
<?php
srand( microtime() * 1000000 );
$num = rand( 1, 4 );
switch( $num )
{
case 1: $image_file = "/home/images/alfa.jpg";
break;
case 2: $image_file = "/home/images/ferrari.jpg";
break;
case 3: $image_file = "/home/images/jaguar.jpg";
break;
case 4: $image_file = "/home/images/porsche.jpg";
break;
}
echo "Random Image : <img src=$image_file />";
?>
</body>
</html>
|
Using HTML Forms
The most important thing to notice when dealing with HTML forms and
PHP is that any form element in an HTML page will automatically be
available to your PHP scripts.
Try out following example by putting the source code in test.php script.
<?php
if( $_POST["name"] || $_POST["age"] )
{
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
|
- The PHP default variable $_PHP_SELF is used for the PHP
script name and when you click "submit" button then same PHP script
will be called and will produce following result:
- The method = "POST" is used to post user data to the server
script. There are two methods of posting data to the server script
which are discussed in PHP GET & POST chapter.
Browser Redirection
The PHP
header() function supplies raw HTTP headers to the
browser and can be used to redirect it to another location. The
redirection script should be at the very top of the page to prevent any
other part of the page from loading.
The target is specified by the
Location: header as the argument to the
header() function. After calling this function the
exit() function can be used to halt parsing of rest of the code.
Following example demonstrates how you can redirect a borwser
request to another web page. Try out this example by puttingthe source
code in test.php script.
<?php
if( $_POST["location"] )
{
$location = $_POST["location"];
header( "Location:$location" );
exit();
}
?>
<html>
<body>
<p>Choose a site to visit :</p>
<form action="<?php $_PHP_SELF ?>" method="POST">
<select name="location">
<option value="http://w3c.org">
World Wise Web Consortium
</option>
<option value="http://www.google.com">
Google Search Page
</option>
</select>
<input type="submit" />
</form>
</body>
</html>
|
Displaying "File Download" Dialog Box
Sometime it is desired that you want to give option where a use will
click a link and it will pop up a "File Download" box to the user in
stead of displaying actual content. This is very easy and will be
achived through HTTP header.
The HTTP header will be different from the actual header where we send
Content-Type as
text/html\n\n. In this case content type will be
application/octet-stream and actual file name will be concatenated alongwith it.
For example,if you want make a
FileName file downloadable from a given link then its syntax will be as follows.
#!/usr/bin/perl
# HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
# Actual File Content
open( FILE, "<FileName" );
while(read(FILE, $buffer, 100) )
{
print("$buffer");
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
No comments:
Post a Comment