Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


PHP #2: Basics filter_list
Author
Message
PHP #2: Basics #1
PHP #2: Basics
By Hackarchives

We shall take an example script to understand PHP.It is the most basic script.

PHP Code:
<?php
$str 
'Aarushi';
echo 
"Aarushi";
echo 
"<strong>Aarushi</strong><br />";
echo 
$str;
echo 
"String is $str";
$str2 'has Dhruv\'s Number';
echo 
$str.$str2;
?>

Let me start from the syntax.
All PHP code must be written between <?php and ?> tags.If code is not written between these tags code will be printed on the screen as plain HTML.

Note: All statements in PHP ends with a semicolon(Wink

$str = 'Aarushi';
In PHP variable names start with $ sign. Variable name must start with a letter or an underscore.Never start a variable name with a number or special symbol.A variable name may contain a number after the first letter though. Also do remember that in PHP variables are case sensitive.So $aarushi and $Aarushi are different variables.
so the statement $str = 'Aarushi'; assigns the string Aarushi to the variable $str.

echo "Aarushi";
echo is used to print anything on the screen.It is the output generator.The string is between 2 quotesWhen PHP processes this statement Aarushi is displayed on the screen of the user.

Note: You may use single quote instead of double but both start and end quote must be same
Note: Echo is not a function.It is a language construct

echo "<strong>Aarushi</strong><br />";
Echo has the ability to print html.So the above statement will display Aarushi in bold and insert a line break after that.

echo $str;
Variables can be outputted simply mentioning it after echo.

echo "String is $str";
Add the variable in double quotes to print the variable with the string.

$str2 = 'has Dhruv\'s Number';
Notice the backslash in this statement.It is an escaping character.In PHP string ends with the second quote but in this line there are three.So we use backslash which escapes the single quote in between and single quote is regarded as a literal character and printed out like other characters

echo $str.$str2;
DOT(.) is a concatenation operator.It joins two variables or strings.so the output of the above will be Aarushi has Dhruv's Number

Exercise:
Write the output for each line.If there is an error write error.Ask any doubts by replying on the thread only
Q1.
PHP Code:
<?php
$str 
'Aarushi';
echo 
$str;
$str2 '\'';
echo 
$str2;
$str3 " body";
echo 
$str.$str2.$str3;
?>

Q2.
PHP Code:
<?php
$str 
"<p id="aarushi">Aarushi</p>";
echo 
$str
$echo $str
.'<br />';
?>

q3.
PHP Code:
<?php
echo "dhruv"."a.k.a.''.'lol";
?>

Hope you liked this tutorial

Reply

RE: PHP #2: Basics #2
Yeah, thanks, is very helply.

Reply







Users browsing this thread: 1 Guest(s)