PHP – Variables

PHP – Variables

 

PHP variables are just a name for a value and you can simply use it like this $var_name = value;. They usually store sting characters and numeric values for later use in the programming block. Here is a simple example:

PHP Code:

<?php
$a 	= "Welcome";
$b 	= "Hello";
$first 	= "John";
$last 	= "Doe";
$c 	= ", how are you today?";

echo "$a $first $last <br />"; 	// Returns: Welcome John Doe
echo "$b $first $c <br />";    // Returns: Hello John, How are you today?

$x=5;
$y=10.1;
echo $x+$y;  // Returns: 15.1
?>

Note: There is no need to previously declare the variable before assigning it a value like in C or Java for example. The type of variable is assigned dynamically depending on its value.

What is a variables scope types in PHP?

In PHP, the scope of a variable is the part of the script where a variable was defined and can be referenced. There are three types of scopes in PHP.

  • local – declared within a function and can only be accessed within that specific function
  • global – declared outside a function and can only be accessed outside a function. There is an exception for this when the global keyword is used. Keep on readding
  • static – declared withing a function and maintaining his value in that function

Special keywords for declaring variables socpes

There are cases when you declare a variable outside of a function and you want to use it inside and the other way around.

This is were the global keyword enters.

PHP Code:

<?php
$x = 1;
$y = 2;

function sum() {
    global $x, $y;
    $result = $x + $y;
}

sum();
echo $result; // Outputs: 3
?>

On the other side static keyword is used to store a value of a varible declared inside a function, for consecutive cals. Normally all the local variable are resseted when the fnction closes, unless static keyword is used.

PHP Code:

<?php
function count_table() {
    static $x = 0;
    echo "Value of x is now $x <br />n";
    return $x++;
}

while(count_table() < 10){
    // do some other stuff
}


/* Outputs:
Value of x is now 0 
Value of x is now 1 
Value of x is now 2 
Value of x is now 3 
Value of x is now 4 
Value of x is now 5 
Value of x is now 6 
Value of x is now 7 
Value of x is now 8 
Value of x is now 9 
Value of x is now 10 
*/
?>

 

Things to remember about variables names

Good things:

  • PHP variables must start with a dollar simbol ($)
  • PHP variables must start (after the dollar symbol) with a letter or underscore ( _ ).
  • PHP variables only admits alpha-numeric characters and underscores (a-z, A-Z, 0-9, or _ ).
  • PHP variables are case sensitive so $myVar si not the same as $MyVar

Bad things:

  • PHP variables can not start with a number.
  • PHP variables names can not contain symbols (except the starting dollar sign).

CamelCase vs snake_case

It is a good to use descriptive-not-to-long names. If your variable name contains various words you can use different techniques to make your programming blocks readable and self self explainable. Regardless of witch one you chose, it is a good practice to stick to it.

  • $snake_case – saparate words using underscore
  • $UpperCameleCase – use capitalization to separate words
  • $lowerCamelCase – same as previous exept that the first letter is a lower case

 

Just chouse one and stick to it acrose your php documents.

Are you kidding me: Variable variables and function variables?

Well, I am not kidding. PHP allows you to use a dynamically assigned name for the variable or a function. Take a look at the flowing code:

PHP Code:

<?php
$a = 'hello';
$$a = 'world';

echo $a." ".$$a."<br />";    // Returns hello world
echo $a." ".$hello;  // Returns hello world
?>

There is a quite cool example of variable variable on the php.net page that I would like to post it here:

PHP<?php
$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";

$a; // Contains Hello
$$a; // Contains World
$$$a; // Contains Foo
$$$$a; // Contains Bar
$$$$$a; // Contains a
?>

You can read more on the php manual site: http://www.php.net/manual/en/language.variables.variable.php

Using a variable as function name can be quite usefull in some scenarious, like hooks, filters, callbacks, and meny others. PHP will try to call the function using the value of your variables.

Variable function example:

PHP<?php
function foo() {
    echo "I am a string inside foo";
}

$bar = "foo";
$bar();    // outputs: I am a string inside foo
?>

This way you can dynamically call one function or another. There are much more examples of implementations and you can read more about function variable on the php manual site: http://php.net/manual/es/functions.variable-functions.php

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars