Sunday, August 26, 2012

String variables in PHP

Hello,

Today I will explain about String variables in PHP, PHP string variables are used to score and manipulate that data. Once we have created a string variable we can manipulate it. We can use string directly into function or it can be stored into variable.

for example:

<?php

$text="My first string variable";
echo $text;

?>

Here we have created s string variable called text and then assign the text My first string variable to it. and then writing the value of the text variable for the output.

There is only one string operator in PHP to join two string values together, It is known as PHP Concatenation Operator.

Here is an example to join two string variables together.

<?php

$one = "My first";
$two = "string variable";

echo $one ." " .$two;

?>


----------------
output:
My first string variable

Monday, August 6, 2012

PHP variables

Today we will learn about PHP variables.

One can use PHP variables to store informations there are some rules for PHP variables such as
  • PHP variable starts with the $ sign, and followed by name of the variable.
           for example: $text
  • PHP variable name much begin with a letter or the underscore character.
  • PHP variable name can only contain alphabetic, numeric characters and underscore.
  • Variable name should not contain spaces.
  • Variable names are case sensitive.
            for example: $text and $TEXT are two different variables.

Here is simple programme for sum of two numbers using variable.

<!DOCTYPE html>
<html>
<body>


<?php
$no1=5;
$no2=10;
$answer=$no1+$no2;
echo
$answer;
?>

</body>
</html>


-------------------------------
output:
15

PHP has 4 different variable scopes such as
  • local
  • global
  • static
  • parameter
Local variables are declared within a PHP function and can only access within that funcion.
Global variable is declared outside of any function and It can be accessed from any part of script.
Static variable are used when you do not want to delete local variable by using static keyword before the first declaration of the variable.
Parameter is a local variable whose value is passed to the function by the calling code.


Saturday, August 4, 2012

Browser detection using PHP

Today I will explain how to check what browser visitors are using with the help of user agent strings our default browser sends as a part of the HTTP request. The variable we can use to check browser is

$_server['http_user_agent']

$_SERVER is a reserved super global PHP variable that contains all the web server informations.

Lets see the example and its output result.


<?php

echo $_SERVER['HTTP_USER_AGENT'];

?>


---------------------
Output

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)