Posted on Leave a comment

Jyrone Parker Live – More Data Types, Flow Control Logic, File I/O

[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]

Three Days In!

Three days in three live streams! In today’s live stream I go over the last couple data types, arrays and closures. Array hold a collection of items such as an array of grocery prices, or an array of color codes. They can be either single dimensional or multi dimensional and they can also be associative. Some examples of arrays include:


$x = [1,2,3,4,5];
$y = [
"favorite_food" => "Fried Chicken",
"favorite_spice" => "Hot Sauce"
];
$z = [
"data" => [
"result" => 1,
"error_code" => 502
],
"message" => "Hello"
];
// Access arrays
echo $x[0]; // first element in array
echo $y["favorite_food"];//access by name
echo $z["data"]["result"]; // accessing multidimensional array elements

Flow Control Logic

I also went over control flow logic. Many times in your application you are going to need to take certain actions based on certain actions the three basic flow control structures are: if/else statements, loops, and switch statements, some examples are as follows.


//if/else statment
if($x[0] % 2){
echo "Even";
}
else{
echo "Odd";
}
//while loop
while($x == true){
echo "Running....";
}
for($i = 0; $i<count($x); ++$i){
echo $x[$i];
}
foreach($x as $key=>$value){
echo $value;
}
 
switch($car->getMake()){
case 'Hyundai':
echo "Hyundai";
break;
case "Ford":
echo "Ford";
break;
default:
echo "IdK";
break;
 
}

 

File I/O

Lastly I went over file input/output including opening files and closing files:


$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

Don’t forget to subscribe to my YouTube channel and to subscribe to this blog to learn more!

Posted on Leave a comment

Crash Course Into PHP – Data Types

[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]

PHP Data Types

PHP variables can store several different data types. Different data types can perform different functions, the basic data types includ:

  • String.e
  • Integer.
  • Float (floating point numbers – also called double)
  • Boolean.
  • Array.
  • Object.
  • NULL.

String

A string is a series of characters wrapped in either single or double quotes, a few examples of strings include:

  • $x = 'Hello my name is Jyrone';
  • $x = "Programming is awesome!"
  • $x = 'The number 13 is very unlucky'

Integer

An integer is any NON-DECIMAL number between -2,147,483,648 and 2,147,483,647 some examples include

  • $x = 1234;
  • $x = -4567;

Float

A float (floating point number) is a number with a decimal point or a number in exponential form some examples include:

  • $x = 1.234
  • $x =1.2e3;
  • $x = 9E-10

Boolean

The boolean data type only has two possible states, true(1) or false(0) some examples include:

  • $x = true;
  • $x = false;

Array

The array data type maps several values to a single variable. Arrays can be initialized with the array() function or the [] shorthand syntax. Arrays can contain other arrays, some examples include:

  • $x = [
    "a" => 1,
    "b" => "xyz"
    ];
  • $x = [
    "a" => [
    "a1" => 2
    ],
    "b" =>345
    ];

Object

The object data type is arguably the most important data type. An object stores data in its properties, and manipulates that data using methods. Objects have to be declared explicitly using the class keyword, a class is a structure that contains methods and properties, an example would be:


class Person{
protected $height, $weight;
public function __construct( $height,$weight){
$this->height = $height;
$this->weight = $weight;
}
public function getWeight(){
return $this->weight;
}
public function getHeight(){
return $this->height;vb
}
public function setWeight($weight){
$this->weight = $weight;
}
public function setHeight($height){
$this->height = $height;
}
}
$person = new Person(70,163);
//returns 70
echo $person->getHeight();

Objects and classes will be covered more in depth as time goes on.

Null

The null data type can only be assigned one value, and that is you guessed it, null. You use null when no value has yet been assigned to a variable:

  • $x = null;

Conclusion

As you progress through my tutorials I will go deeper and deeper into each data type as the need arises in code. Next I will explain the basic operators of PHP. If you haven’t already please subscribe to my newsletter to get real time updates on my blog entries.