Theme Graphic
Theme Graphic

PHP Tutorial

Learn web development using PHP. A brief PHP tutorial.

Subscribe

Author

I am a professional web developer.

Archive

Tags

Posted on Tuesday, December 15, 2009 at 1:10 PM

Variables in PHP

Variables in PHP are denoted by the "$". To assign the value "Hello World" to the variable $str, you simply call it in your code:

$str = "Hello World";

Strings must be enclosed by quotes, but they can contain variables themselves:

$a = 12; $string = "The value of a is $a"; // $string = "The Value of a is 12";

PHP variables do not have to be declared ahead of time, nor do they require a type definition. Note that you may get a warning about using undeclared variables if you try to use them before giving them a value (depending on how you set up error reporting in php.ini). For example:

$a = 4; $c = $a + $b; // $c = 4, but a warning appears "Warning: Undefined variable..". Warnings do not stop a script from continuing. If you forgot to add a semicolon at the end of one of the lines, then you would get a Parser error, which prohibits the script from running.

Since PHP variables are not typed, you don't have to worry about performing mathematical equations on the wrong type, as you might in C...

Posted on Tuesday, December 15, 2009 at 1:02 PM

Introduction to PHP

PHP is a language that was designed to be easily embedded into HTML pages. Most PHP pages have PHP code and HTML intermixed. When a Web server reads a PHP page, it is looking for two things to let it know it should start reading the page as PHP rather than HTML, the start and end PHP tags: <?php and ?>, respectively.

If you have configured your php.ini file to accept "short tags" (which are enabled by default), then you can use the syntax <? and ?> instead. Additionally, you can configure your php.ini file so that it accepts ASP style tags, <% and %>. This feature is turned off by default, and its only real purpose seems to be to allow certain HTML editors to recognize the in-between code as something other than HTML, in which case the editor won't mangle the code by imposing its own set of HTML syntax rules upon the code.

Get Started:

<h1><? echo “First PHP Page.”; ?>!</h1>

The code above, when viewed via a Web server, simply prints out String enclosed in the quotes...

 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.