Essential Things About PHP for PHP Developers

by Hiroshi on June 11, 2008

in PHP Basics

Here are simple but essential things about PHP you will need again and again if you are a PHP developer. e.g. How to use HTML tags in PHP scripts?, How to use line breaks in PHP scripts?, Slashes and Entities in PHP, Define variables in PHP, Get file path in PHP and more…

Using HTML

For using html replace ” “ with ‘ ‘ and store all html in a variable and than you can display that by print or echo.

<?php
$myvar="<font color='#000000'>Hello World</font>";
echo "$myvar";
?>

Line Breaks

For Line break use \n i.e.

<?php
echo "some name \n some other name \n some other name";
?>

Another line break example by nl2br

<?php
$tr = "\n \n \n \n javed \n khalil \n hiroshi \n";
echo nl2br($tr);
?>

Download this tutorial

Using or displaying same things used in php like ; or “”

See ” “ is a thing that php itself uses but what if you want to display or print ” “ in output? In this case use \ i.e.

<?php
echo "Displaying html in php,(replace \"\" with '' in html) \n So i can display \"\" as an output";
?>

\”\” will display actual “” as output.

Slashes and Using Entities

Displaying special html entities as it is and adding ahead of these any text or other things.
& -> &
Now if we use & in php it will give us & output. Sometimes you might want to get it as it is like in some tutorial you might print & instead of & to avoid markup validation error than what you should do?
And how you can get seperated two things in php.

<?php
$tr = "&amp;";
print htmlentities($tr).cc;
?>

Use that entity in a variable and call or print that variable by

<?php
print htmlentities($tr);
?>

Use dot to separate two things in php, like you are printing a variable and ahead of that without space you want to print a statement or something use dot between them like in above example.

Define

<?php
define("CONSTANT", "Hello world");
echo CONSTANT;
// output > Hello world
?>

Get File Path at Server

<?php
print $_SERVER['SCRIPT_FILENAME']
//tells the path of the file
?>

OR

<?php
echo $_SERVER['SCRIPT_NAME']."<br />";
?>

Simple Math In PHP

<?php
$a = "2";
$b = "65";
$c = "7";
$d = "3";
$e = "1";
echo ((($d+$b)/$a)*$c);
?>

Note the use of brackets above.

Customized PHP Info

<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// phpinfo(INFO_CONFIGURATION);
//phpinfo(INFO_GENERAL);
//phpinfo(INFO_CREDITS);
//phpinfo(INFO_MODULES);
//phpinfo(INFO_ENVIRONMENT);
//phpinfo(INFO_VARIABLES);
//phpinfo(INFO_LICENSE);
//phpinfo(INFO_ALL);
?>

Short Tags In PHP

<?= "" ?>

Use of dots In PHP – Concatenation

<?php
echo $_SERVER['SCRIPT_NAME']."<br />";
?>

Comment In PHP Script

# comment
// comment
 
/* comment
and comment
and another line
*/
<?php
$isready ="false";
// we can store bolean(true or false) in a variable
 
$var ="day";
$$var="friday";
// means $day="friday";
 
print $day;
// with print we dont use ""
// in print "" are not used if thats variable in there.
 
// echo is fast than print
// echo accepts html tags, just replace php like tags in html.
?>

Escaping Strings In PHP

\
common escape indicator

\n
new line

\r
carriage return

\t
tab

\\
\

\$
$

\”

isset() In PHP – Checking That If Something Or Some Variable Is Set Or Not

isset() is used to check something whether it is set or not.

If else – elseif – else if In PHP

<?php
 
// if
 
if ( ($a>100) && ($b<4) )
{
do something
}
 
// ifelse
 
if ($a)
{
do something
}
else
{
do other something else
}
 
// if, elseif, elseif, else
 
if ($a)
{
do something
}
elseif($b)
{
do something2
}
else
{
do other thing
}
?>

Redirection In PHP

<?php
header ("location:".$ref);
 
// OR
 
header ("location:https://www.msn.com");
?>

Operators, Conditional operators In PHP

! && || and xor or == === = != !== < > <= >= and loops

Download

Related Posts

Previous post:

Next post: