PHP – Constants

PHP – Constants

 

A constant is a type of variable that cannot be changed. A constant cannot be undefined and once his value has been set, it can not change his value along the script.

Constants are usually found in configuration files containing information such as database user and password, database name, site URL and other useful data.

This is how you define a constant in PHP:

PHP Cost:

<?php

	// site name
	define('SITE_NAME', 'my cool site name', false);

	// database conection
	define('DB_INFO', array(
		'host'=> 'localhost', 
		'usr' => 'my_user',
		'pass'=> 'my_database_pass',
		'db'  => 'my_database')
	);


	echo "Wellcome to " SITE_NAME;
	echo "<br />"
	var_dump(DB_INFO);
?>

Let’s take a closer look at the examples above. We used the define() function to create the constant and with the following params:

  • name – the defined name for our constant
  • value – the defined value for our constant
  • case-sensitive – this is an optional param and it is true by default. This means that our constant is case sensitive. Set it to false if you need it

 

Naming constants

Just like a normal variable a valid constant name starts with a letter or underscore. On the other hand, a PHP constant doesn’t need the “$” sign before the constant name.

Although it is not a must, there is a convention that constant names to be written in uppercase letters. This makes it easy for the identification and distinguishes them from other variables when reviewing coding documents.

Constants scope

Constants are automatically defined as globals and can be directly used anywhere around the script without further action.

PHP Cost:

<?php
	// database connection
	define('DB_INFO', array(
		'host'=> 'localhost', 
		'usr' => 'my_user',
		'pass'=> 'my_database_pass',
		'db'  => 'my_database')
	);

	// simple function too print data
	function nice_printing(){
		echo "<pre>";
		print_r(DB_INFO);
		echo "</pre>"
	}

	// function execution
	nice_printing();
?>

Check if a constant has been defined

Although we have not wet talked about the if/else tag yet, this is a good place to mention that constants have a specific function to check if they are defined.

For variables we will use isset() while for constants we will use defined(). Here is an example using it on a constant:

PHP Code:

<?php
	define('DEBUG_LEVEL', 3);

	if(defined(DEBUG)) {
		// do something if debug is defined
	}
?>

 

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