Showing posts with label Learn PHP. Show all posts
Showing posts with label Learn PHP. Show all posts

Friday, September 13, 2019

Swapping Two Numbers Without Using Third Variable in PHP

Given two variables a and b, swap two variables without using a third variable.



Method (Using Arithmetic Operators)
The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum.



<?php
// PHP Program to swap two numbers without third variable
$a = 25;
$b = 19;

echo "a before swapping : ", $a;
echo "b before swapping : ", $b;

// code to swap numbers

$a = $a + $b; // a becomes 44 now
$b = $a - $b; // b becomes 25 now
$a = $a - $b; // a becomes 19 now

echo "a after swapping : ", $a;
echo "b after swapping : ", $b;
?>

OUTPUT:
a before swapping : 25
b before swapping : 19

a after swapping 19
b after swapping 25

Wednesday, June 26, 2019

echo vs print Statement in PHP

There are two basic ways to get output in PHP:
  • echo
  • print

PHP echo statement

In PHP echo statement is a language construct and not a function, so it can be used without parentheses. But we are allowed to use parentheses with echo statement when we are using more than one arguments with it. The end of the echo statement is identified by the semi-colon (';').
We can use echo to output strings or variables. Below are some of the usage of echo statement in PHP:

Displaying Strings: We can simply use the keyword echo followed by the string to be displayed within quotes. Below example shows how to display strings with PHP:

<?php
echo "Hello, This is echo vs print statement!";
?>

Output: Hello, This is echo vs print statement!

Displaying Strings as multiple arguments: We can pass multiple string arguments to the echo statement instead of a single string argument, separating them by comma (,) operator.

<?php
echo "Hello!", "PHP", "World";
?>

Output: Hello! PHP World

PHP print statement

The PHP print statement is similar to the echo statement and can be used alternative to echo at many times. It is also language construct and so we may not use parentheses: print or print().
Like echo, print statement can also be used to print strings and variables. Below are some examples of using print statement in PHP:

Displaying String of Text: We can display strings with print statement in the same way  as we did with echo statements. The only difference is, we cannot display multiple strings separated by comma(,) with a single print statement.

<?php
print "Hello World!";
?>
Output: Hello World!


Difference Between echo and print Statement

  • echo can accept multiple arguments while print only accepts a single argument
  • echo doesn't have a return value while print always returns 1
  • echo is marginally faster than print


Saturday, April 23, 2016

The Best Way to Learn PHP

PHP is most popular programming language for web development and there are lot of opportunities in PHP now a days. So here we'll discuss what would be the best way to learn PHP and how can we explore our knowledge in PHP.

Learn the language first, not a framework or a CMS.
Below is a basic outline of what you'll need to know. Use this as a guide to help you find the information you need. Sorry for not posting any actual resources, but you can consider this a basic syllabus.

Basics

Like anything. Walk before you run. There is a LOT to the "basics" of any language, but these four are where you will derive most value out of PHP. Practice these things until you can do them in your sleep.

The syntax (variables, arrays, if/else, loops, objects, interfaces etc)
Doing stuff with $_GET and $_POST variables, and generally understanding how a request & response in PHP works.
How to perform SQL queries with PDO (don't bother learning mysql, as it's deprecated. Also recommend avoiding mysqli, as its a dated procedural way to code)
How to work with sessions & cookies

Security

Key security concepts to understand when building a PHP application. This is NOT a complete list, but it accounts for most of the vulnerability points an application can make. This is after the basics because it's that important - knowing this stuff is simply not optional.


  • Cross Site Request Forgery protection (CSRF)
  • Filtering input to prevent malicious code execution
  • Filtering input and using PDO prepared statements to stop SQL injection
  • Escaping output to mitigate Cross Site Scripting vulnerabilities (XSS)
  • How to hash and store passwords (php 5.5 made this a lot easier)
  • How to safely upload images & files
  • Configuring your environment for maximum error checking


Object Oriented Design

Once you're comfortable with the basics & security, you should start thinking in terms of objects, and read up on/practice the following concepts. Like the above, do these until they're second nature.
S.O.L.I.D. principles - S is the most important.

S – Single-responsiblity principle
O – Open-closed principle
L – Liskov substitution principle
I – Interface segregation principle
D – Dependency Inversion Principle


  • Programming to an interface
  • Favoring composition over inheritance
  • Dependency injection
  • Namespaces
  • Exceptions (not technically part of OO Design, but it's a bit too advanced


Application Architecture


This refers to how you would structure an application from top to bottom.
MVC pattern (and in general, layered architecture)
Basic understanding of what is meant by a domain and Domain Driven Design (this is a big topic, but you should at least understand the basics)
Using an Inversion of Control container for managing dependency injection (start with Pimple, then checkout Laravel's IoC)
Autoloading & using composer to manage external libraries and dependencies
Various design patterns
Unit testing

Frameworks

Knowing all of the above will help you take greater advantage of (and better appreciate) frameworks. It's easy to do a very bad job using a good framework if you don't quite understand the sections above. Frameworks aren't magic bullets - they require prior knowledge to be used effectively.

There are many frameworks to choose from. The most popular full featured frameworks are Laravel and Symfony, but you should dabble in a wide variety of them to get a sense for how they solve common problems.
There are many more nooks and crannies in PHP than what I've outlined above, but that should be enough to get you started. The key to all of the above is practice and tinkering.

If you're really a beginner, W3Schools (http://w3schools.com) will help you master PHP by using step by step tutorials. This is helpful while so many people want to get their feet wet on PHP didn't know where to start.

If you've mastered the basic and want to advanced to the next level, the PHP website itself (http://php.net/manual/en/index.php) will give you everything you need, all in one place.