ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Basic PHP

Updated on January 28, 2009

Basic PHP

The leading webscripting language of today is PHP, short for 'Personal Homepage Preprocessor'. It can do all kinds of thing for you, as easy as finding out what date it is, send emails to an email list, or it can run a whole game for you.

I will teach you the very basics of this programming language to get you started. Please note that you will not master this language in a matter of hours, but having all the basics in one place will hopefully help you in becoming a great webmaster.

PHP can be written in all simple text editors (I use regular notepad for editing, and 'Notepad++' when I need to find line numbers. Okay, let start!

Basic Knowledge

Requirements

PHP requires you to have a PHP enabled web server. Ask your hoster if you can run PHP, or download a free webserver tool. Google it.

A PHP file should be saved as 'filename.php'. 

Outputting 

To make your webserver notice you are going to use PHP code, find the place where you want it to be executed and write:

<?php
?>


All the PHP code you are going to use, should be placed between the PHP open ('<?php') tag and the PHP end ('?>') tag.

Now you know how to open and close PHP, you also want to know how to make it do something. The basic method of making php output something is 'echo'. You use it like this:

<?php
echo "Hello world!";
?>

This would, indeed, output 'Hello world!' to the browser. It is also very handy to notice that PHP can therefore also output raw HTML. Example:

<?php
echo "Hello <b>world!</b>";
?>

This would output 'Hello world!'.

Variables

In PHP you can link information to a so called 'variable'. A veriable is usually in the format '$name = "Peter Griffins"'. So if you'd want to use this in a script, it would look like this:

<?php
$name = "Peter Griffin";
$age = 40;
echo $name." is ".$age." years old.";
?>

This would output 'Peter Griffin is 40 years old.'. Please note that in PHP the method to join two or more pieces together is the full-stop, or '.'. Also note that $age = 40; doesn't need quotes because it isn't a string, but an integer. If you would write it as $age = "40"; it would work, but you wouldn't be able to make PHP do maths with it.

Variables can have indexes. So $name could have more than just 1 string attached to it. Indexes in PHP are used as '[2]' behind a variable. Indexes in PHP start at 0. So you could use it like this:

<?php
$name[0] = "Peter Griffin";
$name[1] = "Stewie Griffin";
echo $name[1]."' dad is named '".$name[0]."'";
?>

This example would output "Stewie Griffin' dad is named 'Peter Griffin'".

Maths

I told you about doing maths with PHP above, I will show you the basics of using maths in PHP. First you'll need a list of the basic commands you can use.

  • / = divide
  • * = multiply
  • + = add up
  • - = substract
  • ^ = involution
  • sqrt() = square root (example: sqrt(144) = 12)

I will show you an example on how to use this.

<?php
$a = 5;
$b = 8;
$c = 3;
$sum = $a+$b+$c; //sum = 5+8+3 = 16 (By the way, this is a php 'comment', it gets ignored by PHP)
echo "The sum of ".$a.", ".$b." and ".$c." is <b>".$sum."</b>.";
?>


This would output 'The sum of 5, 8 and 3 is 16.'. Of course you can also use the other commands available. It would look something like this:

<?php
$a = 80;
$b = 20;
$c = 10;
$answer = $a+$b/100*$c;
echo $answer;
?>

This would run 80+20/100*10, outputting '10'.

Conditions

if()

PHP uses the command 'if()' to check if a statement or condition is true. It could, for example, check if a user is logged in or if a certain number equals another number. After an if statement you would tell PHP what to do, and then you close the if statement. Example:

<?php
$a = 5+4;
$b = 9;
if($a == $b) {
  echo "5+4 equals 9!";
}
?>

As you can see, this would output '5+4 equals 9!' if 5+4 = 9 (Which is does). '==' is a PHP command for 'equals to'. I will list some other forms of checking conditions.

  • != = Other than
  • <  = Smaller than
  • >  = Bigger than
  • <= = Smaller than or Equal to
  • >= = Bigger than or Equal to

So if you'd want to check if variable a is smaller than variable b, you'd use this script:

<?php
$a = 5;
$b = 8;
if($a < $b) {
  echo "5 is smaller than 8!";
}
?>

This would output '5 is smaller than 8!' if the condition 5 < 8 is true.

Elseif

PHP uses 'elseif' to add another check on the if statement. It will first check the if statement, and if it it returns false, it will check the following elseif statement, if that one returns false, it can check the elseif statement after that (if you have one). You would use it like this:

<?php
$a = "This is a string!";
$b = "This is a different string!";
$c = "This is another string!";
$d = "This is a string!";

if($a == $b) { 
  echo "String A and B match.";
} elseif($a == $c) {
  echo "String A and C match.";
} elseif($a == $d) { 
  echo "String A and D match.";
}
?>

This would output 'String A and D match.'.

Else

PHP will execute the commands after 'else' if no conditions return true. You would use it like this:

<?php
$a = 15;
$b = 15.5;
$c = "Haha!";

if($a == $b) {
  echo "Variable A is equal to variable B";
} elseif($a == $c) {
  echo "Variable A is equal to variable C";
} else {
  echo "Variable A is not equal to variable B nor C";
}
?>

This would output 'Variable A is not equal to variable B nor C';

That's it, you've just learned the very basics of PHP! Now you know this you'll have a basic understanding of how PHP scripts work. I highly recommend you to google for some free and simple PHP scripts and edit them. That's how I learned PHP.

 

PHP Logo
PHP Logo

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)