Coder Social home page Coder Social logo

php-course's Introduction

PHP Project

Instructions

This is the template you will use to track the changes in our PHP tutorial. Clone this file in the www folder in your WAMP or MAMP server.

php-course's People

Contributors

grobergm avatar jdegand avatar

Watchers

 avatar

php-course's Issues

Conditional Statements

Conditional Statements

"If" statements are one of the most critical procedures in php decision making. They give you control over what is displayed, making the display more dynamic.

An "if" statement tests if a condition is true or false. If true, it will run the code following it. If false, it will skip that code and optionally run code in an "else" statement.

Build your "if" statement

We can use an "if" statement to display products we can afford. Let's make another foreach loop, but this time test if the value is less than or equal our credit variable.

Add the following to the end of your php tag:

echo "<h2>Items you can afford</h2>";

foreach($products as $key => $value){
  if($value <= $credit ){
  	echo "<p>".$key."</p>"; 
  }
}

Refresh your page, and make sure these products are listed: "Computer, iPhone, and Toaster".

Your code should look like this:

<!DOCTYPE html>
<html>
  <head>
    <title>PHP Store</title>
  </head>
  <body>
    <?php
	    $name = "PHP Store";
			$credit = 1000; 

	    echo "<h1>Welcome to ".$name."!</h1>";
	    echo "<h2>You have $".$credit." in your wallet.</h2>";

		  $products['Computer']=750;
	    $products['Car']=15000;
	    $products['iPhone']=1000;
	    $products['Toaster']=75;

	    foreach($products as $key => $value){
		    echo "<p>The ".$key." costs ".$value."</p>";
	    }

	    echo "<h2>Items you can afford</h2>";

	    foreach($products as $key => $value){
		    if($value <= $credit ){
		    	echo "<p>".$key."</p>"; 
		    }
	    }
    ?>
  </body>
</html>

When you are finished with this section, push your file to GitHub for the next step:

git add index.php
git commit -m"add conditional statement"
git push origin master

Conclusion

Conclusion

At this point we just need to add all the pieces together and we will have a functioning program that displays products with added tax. Your code should look like this now:

<!DOCTYPE html>
<html>
  <head>
    <title>PHP Store</title>
  </head>
  <body>
    <?php
	    $name = "PHP Store";
			$credit = 1000; 

	    echo "<h1>Welcome to ".$name."!</h1>";
	    echo "<h2>You have $".$credit." in your wallet.</h2>";

		  $products['Computer']=750;
	    $products['Car']=15000;
	    $products['iPhone']=1000;
	    $products['Toaster']=75;

      $taxRate=0.0825;

      function tax_calc($amount,$tax){
	    	$addedTax = $amount*$tax;
	    	$amountWithTax = round($amount+$addedTax,2);
	    	return $amountWithTax;
    	}

	    foreach($products as $key => $value){
	    	$costWithTax = tax_calc($value,$taxRate);
		    echo "<p>The ".$key." costs ".$costWithTax." with tax</p>";
	    }

	    echo "<h2>Items you can afford</h2>";

	    foreach($products as $key => $value){
	    	$costWithTax = tax_calc($value,$taxRate);
		    if($costWithTax <= $credit ){
		    	echo "<p>".$key."</p>"; 
		    }
	    }
    ?>
  </body>
</html>

... ๐Ÿค”
Looks like we can't afford the iPhone after all (after taxes)!

Close this issue when you are finished

Installation

Initial Setup

PHP is a server-side language, so in order to make your code work you must put it on either a web-server or a local server. For the purposes of this tutorial we will install a local server called WAMP (for Windows users) or MAMP (for Mac users). You can also install LAMP server, if you use a Linux.

Noticing a pattern in these names? The only difference is the operating system. The -AMP part stands for Apache, MySQL, and PHP. We are using WAMP in this turotial, but use the version that matches your OS.

Make sure you have a text editor installed before proceeding (such as Atom, Sublime Text, or Notepad++).

Install WAMP server (Windows)

  • Go to http://www.wampserver.com/en/ and click "START USING WAMPSERVER"
  • Choose either the 64bit or 32bit version. (Most machines should run the 64 just fine but if in doubt choose 32)
  • Once your server is installed open a browser and go to http://localhost if it works then your install is finished.
  • Make sure to note where your php files are located in WAMP ie: C:\wamp64\www (move or delete all files in this folder now)

Install MAMP server (Mac)

Clone course repository

You will be storing the code for this project in this repository. Before we go any further, navigate to the www folder in your terminal (which should be empty now). Clone your template project into this folder, using git:git clone https://github.com/jdegand/php-course.git

Refresh your browser, and make sure you see folder for the php-course. If it worked, close this issue for the next step.

Building Array Variables

Building variables with an Array

Arrays are groupings of information that can be tied to a variable. Arrays have keys and keys have values. For example:

  $ArrayVariable['keyName'] = 'something';

Build an array for a series of products and their prices under the variables we created in the previous step.

  $products['Computer']=750;
  $products['Car']=15000;
  $products['iPhone']=1000;
  $products['Toaster']=75;

You can access the value stored in an array like this:

echo "<p>A car costs ".$products['Car']."</p>";

Your code should look like this now:

<!DOCTYPE html>
<html>
  <head>
    <title>PHP Store</title>
  </head>
  <body>
    <?php
      $name = "PHP Store";
      $credit = 1000; 

      echo "<h1>Welcome to ".$name."!</h1>";
      echo "<h2>You have $".$credit." in your wallet.</h2>";

	    $products['Computer']=750;
	    $products['Car']=15000;
	    $products['iPhone']=1000;
	    $products['Toaster']=75;

	    echo "<p>A car costs $".$products['Car']."</p>";
    ?>
  </body>
</html>

Save your file and refresh your browser. You should see a new sentence on your web page.

Copy and paste the new sentence as a comment on this issue for the next step.

Creating PHP Variables

Creating Variables

Building Variables

Variables are an essential part of any project. They hold data that can be modified or used later in a program.

We define variables in PHP with a $ sign and then we need to decide what the datatype for the variable will be. For a string of text make sure to put the text in quotes. If the value is a decimal or integer, no quotes are needed.

For Example, let's add these after the <?php in our code:

$name = "PHP Store";
$credit = 1000;

Combining text and Variables

Using a period (.), we can concatenate text and variables. Replace the echo line with the following:

echo "<h1>Welcome to ".$name."!</h1>";
echo "<h2>You have $".$credit." in your wallet.</h2>";";

Your code should look like this now:

<!DOCTYPE html>
<html>
  <head>
    <title>PHP Store</title>
  </head>
  <body>
    <?php
      $name = "PHP Store";
      $credit = 1000; 

      echo "<h1>Welcome to ".$name."!</h1>";
      echo "<h2>You have $".$credit." in your wallet.</h2>";
    ?>
  </body>
</html>

Refresh your browser and you should these sentences: Welcome to PHP Store! You have $1000 in your wallet.

If that worked, push your file to GitHub for the next step:

git add index.php
git commit -m"add variables"
git push origin master

Writing a Math Function

Functions

Functions are pre-built commands in PHP that do some of the hard work for you. You can also write your own custom function. The function we will write will calculate tax, but first let's go over using math in PHP

Using Math

By using math symbols you can add(+), subtract(-), multiply(*) or divide(/) variables. Add this to the bottom of your php tags:

$amount=800;
$taxRate=0.0825;
$addedTax= $amount*$taxRate;  //amount = 800, tax = .0825
echo $addedTax;

Refresh the page, and leave a comment with the value of $addedTax to continue.

Setting up PHP

Setting up PHP

PHP is server-side code that runs on top of a normal HTML website. This means you can use HTML and PHP together.

  • Get started by creating a PHP document in the www folder in WAMP. Index.php is considered by most websites to be the "home page"
  • Open index.php in your text editor and type the following HTML code
<!DOCTYPE html>
<html>
  <head>
    <title>PHP Store</title>
  </head>
  <body>
    <?php
    // Using two forward slashes we create comments in php.
    // Within the php tag, we can insert values into our HTML.
    echo "<h1>Welcome!</h1>";
    ?>
  </body>
</html>

Displaying Content

In this example, we used the "echo" command to insert an HTML header within the php tags. All of our php code will be contained in these tags.

Make sure you always end every statement with a semicolon

In your browser, click on the php-course folder, and you should see the header we inserted being displayed.

If it worked, save your changes to GitHub.

git add index.php
git commit -m"initial php setup"
git push origin master

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.