Posted on Leave a comment

Jyrone Parker Live – Buidling Our First PHP Application (Fibonacci)

[youtube width=”100%” height=”100%” autoplay=”false”]https://www.youtube.com/watch?v=-77pgzODEQI[/youtube]

What Better Assignment Than Fibonacci?

Since I am finished with the beginning into to PHP videos I found it imperative to do a stream putting everything together and showing you how easy it is to write an app. In today’s live stream I create a Fibonacci app, for those who are not familiar with the Fibonacci sequence there is a great wiki here. In this application I demonstrate variables, control flow logic, exception handling, and function design. All my code can be seen on my Github here or you can view it below in text or on video

<?php
$input = '';
$running = true;
while($running){
try{
getInput($input);
echo calculateFib($input) . "\n";
}
catch(Exception $e){
echo "Message: {$e->getMessage()}";
}
}
function getInput(&$input){
$input = readline("Please input your number or q to quit: ");
};
function calculateFib($input){
if(intval($input) < 0){
throw new Exception("Input must be greater than zero! \n");
}
elseif($input === 'q' || $input === 'Q'){
exit();
}
if(intval($input) === 0 || intval($input) === 1){
return 1;
}
else{
return calculateFib($input-1) + calculateFib($input - 2);
}
};
?>

 

Posted on Leave a comment

Crash Course Into PHP – Syntax

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

By now you guys should know

That I specialize in Laravel development, it’s what 90% of my blog content comprises of. I want to kick off 2017 with my new online coding classes, these will be Youtube series (mostly screen recordings, but a few face to camera as well you can subscribe to my channel here), designed to teach coding to beginners as well as advance methodologies for more seasoned programmers. The main concern I receive from newcomers coming to my site, is that they would like to see more basic PHP tutorials. As I thought about it, I couldn’t agree more, thus today starts the crash course in PHP lessons. I say crash course because I’m only going over the basic concepts, enough where the Laravel code will start making more sense to the n00bs.

What Is PHP?

PHP is a server-side scripting language designed primarily for web development but also used as a general-purpose programming language. It powers the majority of websites in the world (82.3%). The largest social media platform (Facebook) is powered by PHP, among others. It by far has the most support libraries than any other language. It is free to use and develop with both for personal and commercial applications, making it ideal for programmers with a low budget. PHP code can be directly embedded into HTML, making it ideal to create rich dynamic web pages (and apps).

How To Install PHP

Anyone who knows me, knows I love virtual machines! It makes installing software such as PHP and a full LEMP stack a breeze. Since learning Laravel is the ultimate goal, I will direct you to install the Laravel Homestead virtual machine on your computer, it has the following software already bundled in, meaning you won’t have to do a lick of package management:

  • Ubuntu 16.04
  • Git
  • PHP 7.0
  • Nginx
  • MySQL
  • MariaDB
  • Sqlite3
  • Postgres
  • Composer
  • Node (With PM2, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd

Mac users have the option of using Laravel Valet as well.

PHP Syntax

All PHP files end in .php which shouldn’t be too surprising. Do me a favor, open up your terminal (log into the virtual machine) and create a new php file

nano hello.php

Now paste in the following content and let’s break it down:


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

The first and last parts of this script tell the preprocessor that PHP code needs to be interpreted


<?php
// PHP code goes here
?>

The middle part uses the built-in PHP function (we will get to functions later) echo to print the text “Hello World!” to the screen
The semicolon tells the preprocessor that the PHP statement is completed.
If you run the following command you should see the text “Hello World!” pop up.

php hello.php

The really cool thing about PHP is that you can embed it straight into your HTML, so if you wanted to show this same snippet of code in a webpage instead of the terminal you can simply do this:

HTML Embedding


<!DOCTYPE html>

 

<html>

 

<body>

 

<h1>My first PHP page</h1>

 

<?php echo “Hello World!”; ?>

 

</body>

 

</html>

PHP Comments

Often times when coding you need to leave a note to yourself or fellow programmers. In PHP there are two ways to leave comments in your code

  • // – Creates a SINGLE LINE comment
  • # – Creates a SINGLE LINE comment
  • /**/ – Creates a MULTILINE comment that spans several lines

<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>

This is the most basic syntax of PHP, next we will go over variables and data types. If you haven’t please subscribe to my newsletter to get real time updates when I push new material.
 

Posted on Leave a comment

API Authentication In Laravel 5.2

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

Hours Saved!

Token based API authentication is something you are inevitably going to encounter if you plan on working with web/mobile apps (unless you are coding under a rock). The benefits are great, in fact here are six of them; However it can be a nightmare if you need to create your own token authentication server. As of Laravel 5.2, there is a new auth guard called conveniently api this guard allows us to check an api_token parameter against an api_token field on our users table in our database.

API Configuration

Almost everything is configured right out of the box, open up config/auth.php and you will see this block of code

  /*
 |--------------------------------------------------------------------------
 | Authentication Guards
 |--------------------------------------------------------------------------
 |
 | Next, you may define every authentication guard for your application.
 | Of course, a great default configuration has been defined for you
 | here which uses session storage and the Eloquent user provider.
 |
 | All authentication drivers have a user provider. This defines how the
 | users are actually retrieved out of your database or other storage
 | mechanisms used by this application to persist your user's data.
 |
 | Supported: "session", "token"
 |
 */
 'guards' => [
 'web' => [
 'driver' => 'session',
 'provider' => 'users',
 ],
 'api' => [
 'driver' => 'token',
 'provider' => 'users',
 ],
 ],

By default Laravel 5.2 uses the web guard, which is the traditional session based model. This model is great for a traditional web application but not suitable for API architecture. The next guard is the api this guard is suitable for routes that need to be consumed by mobile apps and the like. The reason why you would want to do this is because when you are making request from your client-side app, each request is mutually exclusive of the requests before and after it, thus each one requires authentication. In the traditional session based flow, user data is saved on the server and that session data is used in every request, not only is this approach not scalable, it isn’t efficient or as secure as one might think. API token authentication is faster, scalable, and more secure. The biggest benefit of all is that you can now use this token in other applications to access your own API if you so choose. If you would like to see an example leave a comment of what kind of app you would like to see this implemented in and I will write it, record it, and blog it!