Wednesday, 12 March 2014

Introduction to PHP

Introduction to PHP

PHP is a programming language that can do all sorts of things: evaluate form data sent from a browser, build custom web content to serve the browser, talk to a database, and even send and receive cookies..

Why Learn PHP?
"So what?" You might say. "I can do that with JavaScript." And that's true! But JavaScript's knowledge can be limited.
JavaScript generally runs in the browser, orclient. This means it only really knows what's going on in your browser, plus whatever information it gets from the website(s) you're connecting to.
PHP, on the other hand, runs on the same computer as the website you're visiting, which is known as the server. This means that it has access to all the information and files on that machine, which allows it to construct custom HTML pages to send to your browser, handle cookies, and run tasks or perform calculations with data from that website.
PHP and HTML
PHP code can be written right into your HTML, like this:
<body>
  <p>

    <?php
      echo "I'm learning PHP!";
    ?>

  </p>
</body>
Your PHP code goes inside the <?php and ?> delimiters. Here we use the function echoto output I'm learning PHP!. We also end the line with a semicolon.
PHP Files
You might have noticed that our main file is now index.php instead of index.html. This is important! It tells the PHP interpreter that there's PHP code in the file to evaluate.
Echo
The echo function outputs strings. If you type
<?php
  echo "Hello!";
?>
PHP will output Hello!.
Make sure to end your line of PHP code with a semicolon.
Strings
A string is a word or phrase between quotes, like so: "Hello, world!"
You can type a string all at once, like this:
<?php
  echo "Hello, world!";
?>
Or use the concatenation operator, which glues several strings together:
<?php
   echo "Hello," . " " . "world" . "!";
?>
The concatenation operator is just a dot (.). (If you're coming to PHP from JavaScript, the dot does the same thing for strings that +does in JavaScript.)
Variables
So far we've been outputting strings and doing math.
To do more complex coding, we need a way to "save" these values. We can do this usingvariables. A variable can store a string or a number, and gives it a specific case-senstive name.
Examples:
  • $myName = "Beyonce";
  • $myAge = 32;
All variable names in PHP start with a dollar sign ( $ ).
Semicolons
You've probably noticed that our lines of PHP code end in semicolons (;). PHP requires semicolons at the end of eachstatement, which is the shortest unit of standalone code. (For example, echo"Hello!"; or 2 + 2;)
You can think of a statement is a complete PHP thought. 19 + or echo aren't complete thoughts, so you wouldn't put semicolons at the end of them!
<?php echo "Use your semicolons!"; ?>
Comments
Just like we sometimes put comments in our CSS (using /* this syntax */) or in our HTML (using <!-- this syntax -->), we can also put comments in our PHP code! We do that using two forward slashes (//), like so:
<?php
    echo "I get printed!";
    // I don't! I'm a comment.
?>


Reference : Click Here

0 comments:

Post a Comment

 
;