Variables in PHP

variables in php 

 declaring variables in php is very simple .its more easier then java and c++ to declare variable

•PHP variables must begin with a “$” sign 

ie $x         , $y

•Case-sensitive 

($Foo != $foo != $fOo) 

•Global and locally-scoped variables 

–Global variables can be used anywhere
–Local variables restricted to a function or class

•Certain variable names reserved by PHP

–Form variables ($_POST, $_GET)
–Server variables ($_SERVER)
–Etc.

•PHP is a loosely typed language

–In PHP, a variable does not need to be declared before adding a value to it
Example code
[code start]

<?php
$foo = 25;  // Numerical variable
$bar = “Hello”;  // String variable
$foo = ($foo * 7);  // Multiplies foo by 7
$bar = ($bar * 7);  // Invalid expression
?>

[code end]

Echo in php 

•The PHP command ‘echo’ 

is used to output the parameters passed to it
–The typical usage for this is to send data to the client’s web-browser

•Syntax

–void echo (string arg1 [, string argn...])
–In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function 
Example:

[code start]

<html>
<body>
<?php
echo "Hello World";
?>
 <br />
<?php
$txt="Hello World";
echo $txt;
?>
</body>
</html>

[code end]

Example2:
[code start] 
<?php
$foo = 25;  // Numerical variable
$bar = “Hello”;  // String variable
echo $bar;  // Outputs Hello
echo $foo,$bar;  // Outputs 25Hello
echo “5x5=”,$foo;  // Outputs 5x5=25
echo “5x5=$foo”;  // Outputs 5x5=25
echo ‘5x5=$
foo’;  // Outputs 5x5=$foo
?>

[code start] 

•Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25
•Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP
•This is true for both variables and character escape-sequences (such as “\n” or “\\”)


0 comments: