Posts

Showing posts from June, 2025

Including libraries in programming

Libraries is a code that someone else wrote and these had been well tested so it is possible to rely on functionality of these. Libraries allow a computer developer to focus on implementation details of the specifics without spending much time on functionality provided by libraries. A developer needs to know which functionality is available in which library and how to use that functionality. Different programming languages call these extensions differently. In some programming languages these are called libraries, in some they are called extensions or modules. Libraries are usually very specific, they provide functionality for certain actions. If a different functionality is needed, then it maybe exists in a different library, or it is needed to implement this functionality from scratch. If a person will try to use a function that was described in a library, but never that library was included in the code of the program, then this program will either fail compilation or execution.

Checking if the key exits in PHP’s array

It is possible to check if a key exists in a PHP array. This is done using array_key_exists method. This method accepts two values. First one is a variable that contains a key to check, and second one is the name of the array. This method will return true if the given key exists in the array, and it will return false if it is not there.

Looping through an array in PHP

It is possible to loop through all items of an array in PHP using loop with foreach statement. In this case each element of the array will be assigned to a variable that is used in the array.

var_dump in PHP

var_dump function in PHP will display more information. It will not look pretty, it is good for debugging purposes.

Printing an array in PHP

It is possible to print an array in PHP using print_r statement. It will not look good. If your web application requires a nicer look of displaying information then think of other methods. print_r statement is good for debugging to see which values are stored inside of an array.

Declaring a PHP array

It is possible to declare a PHP array by two ways. First one is to include PHP array elements in square brackets, second one is to include array elements in the array() keyword. Square brackets is being an older style of declaring a PHP array. YouTube video

Escape sequences

Escape sequence is a list of characters that represent special actions in a computer. For example in PHP \n represents a new line, \t - is a tab symbol, \\ - represents backslash symbol, \" is quote symbol, \r is a carriage return, it moves cursor to a new line. By default these characters will break functionality of an application, therefore to avoid issues with these, escape sequence can be used. For these special sequences to work, a string must be unclosed in a type of quotes that support special sequences. For example in PHP these are double quotes. Single quotes can also be used to represent a string, however single quotes do not support special sequences. YouTube video

FLOP

FLOP stands for Floating Point Operations a Second. A related term is Instructions Per Second (IPS).  According to Google, human brain has computational power of about 1 exaFLOP, or 10 to the 18th power. Modern super computers can achieve this performance, however they require a lot more power. They require megawatts of power, where human's brain requires only about 20 watts. YouTube video

GUI vs CLI performance

To use GUI (Graphical User Interface) a user needs to do point and click with the mouse cursor. Even so it maybe a preferred way for a novice computer user, however it maybe efficiency loss for an experienced computer user. In addition to that GUI will utilize more system resources, by displaying graphical elements. CLI does not do that, therefore it is more efficient. One more thing. To connect remotely and transfer graphical elements require more bandwidth than transfer text data. It is possible to save on connectivity resources by transferring only text data. Windows Server Core installs without graphical elements, therefore more system resources are allocated to the system, rather than displaying graphical elements.

Anonymous or lambda functions in PHP

It is possible to assign a PHP function to a variable. If a variable will be called, then this function will execute.

Handling a specific type of exceptions in PHP code

By default try/catch block will handle all the errors. What to do if a specific error type needs to be handled, but if a different error happens, then a different code needs to be executed. In this case it is possible to specify which type of exception will be handled by an except block. Name of the exception needs to specified in the parentheses that followed catch statement. Exception message will be stored in a variable that follows that exception type.

finally block

I had described try/catch block in the previous article. If catch block itself will report an error, then it is possible to execute code within finally block.

try/catch PHP block

try/catch is PHP block to handle errors. A programming operation where the error may occur is placed within try block. If error occurs then code within catch block will be executed. If there is no error, then code within try block will be executed.

What GUI and CLI stand for?

GUI stands for Graphical User Interface, CLI stands for Command Line Interface. Desktop users prefer to use GUI, because it looks good, however developers on Linux may prefer using CLI, because it allows for faster development. It is possible to use keyboard shortcuts instead of point and click. GUI uses more system resources, therefore CLI is desired for server operations. It possible to create batch files in CLI to execute commonly used sequence of commands.

A compiler

A compiler translates code from human readable code into machine code. A compiler is programming language specific, it not possible to take a compiler that deals with assembly code and compile with it C application. A compiler is also platform specific. It is not possible to use application compiled for Mac OS and use it in Windows OS.

Functions in programming

It is possible to separate commonly used code in a program into a separate function. It is easier to debug code with functions. If a problem exit within a function, then only code of that function needs to be fixed all other code may stay impact, I say may, because I assume that there are no more code issues. A function needs to have an input and a result. An example of a function is a sum of two numbers. In this case two numbers will be provided to a function, and result of it will be addition of these numbers.

Boolean expressions

Boolean expressions have result of either true or false. False is represented by 0, and True is represented by 1. Boolean logic is named after mathematician with the last name Bool. If big letter A is compared with another big letter A, than result of this operation will be true, however if letter A is compared with letter B, then result of this operation will be false.

Pseudocode

Pseudocode represents how the computer program will be solved without implementation details of a specific programming languages. Pseudocode outlines steps that are needed to take to solve a compute problem. Pseudocode has more details than a diagram. If a diagram represents logic, than pseudocode represents how this logic will be implemented. Pseudocode is an optional step, often experienced developers skip this step in favor of actual development.

Images and videos

Images are collection of bytes, with each byte representing a color. If these images move or are replaced with other images then we receive a video. You are probably familiar with a term FPS. FPS stands for frames per second - of how many times each second a picture will be replaced by another picture. There different types of image and video formats. I will not be covering those details here.

A pixel

If we zoom in or get close to a screen we can see pixels. Pixels allow our displays to represent color. Just like ASCII symbols are represented by bits, in similar way work displays. Each pixel will be illuminated with certain amount of red color, green and blue. Absence of a color means black, and light up these color to the max gives us white color. It is possible to have different colors by lighting up certain RGB colors. For example if colors red and green are turned off, and only blue is light up, then color blue will be illuminated. It is possible to have light or dark blue color depending on how much power is provided to this color element. You are probably familiar with this definition. Screen size is so many pixels horizontally and so many pixels vertically. Number of pixels, represent resolution of a screen. Typically the more pixels you have in a screen, the better quality that screen is.

A byte

A byte is a combination of 8 bits. With 8 bits it is possible to have 256 possible combinations. You are probable familiar with terms like gigabyte or terabyte. These terms represent capacity of a storage or computer memory.

What is a bit?

A bit is the smallest amount of information in a computer. A bit is represented with 0 or 1. It has two possible choices. In hardware 0 represents an absence of a signal and 1 represents availability of it. Binary logic or 0s and 1s is implemented at the hardware level by transistors.

Unicode

To type in different language other then English it is possible to use UTF system. UTF translates as Unicode Transformation Format. There are different versions of UTF, depending on how many bits are included in UTF standard. There are UTF-8, 16 and 32. UTF systems not only include support for different languages, but they also support different symbols such smiley face,  sad face and so on.

ASCII and Unicode

ASCII represents first 128 characters. ASCII stands for American Standard Code for Information Interchange. It is possible to use extended ASCII characters from 128 to 255. ASCII is a mapping between numbers and symbols. ASCII is a standard, so computers adhere to it. ASCII preforms mapping between numbers and characters. Alphabet that is used in ASCII is English alphabet. However computers can use other languages. This can be done with Unicode. Current version of Unicode supports almost 155K of symbols. These symbols are not just alphabet characters, these are symbols such as smiley faces, sad faces, faces of different colors, and so on.

A byte

A byte is a collection of 8 bits. Symbols such a characters, numbers, numerical operators, special characters can be represented with 8 bits. It is possible to represent 256 characters with 8 bits. 

Unit tests

Unit tests is another way to validate if the code does what it's supposed to. Unit tests must be comprehensive and cover if not all, then majority of code that is implemented. Release of code to production needs to depend on successful testing of the code. If tests fail, then it means that code has a bug, and is not ready to be promoted to production.

Documenting code

In addition to beautiful code it is nice to have documentation and/or flowcharts that will explain functionality of the software. Documentation and flowcharts can help to identify logical errors with the code. Without either documentation or a flowchart good luck funding such an error. If you are busy working on coding things, at least some idea of what is done needs to be documented. Without having this, it will be difficult to tell if the code does the right thing.

Beautify code

If you are the only developer, you probably do not care how the code look like, however if you are not the only one and there are other people who will look at the code, than it makes a good idea to write beautiful code that is understandable by many people. Even if you are the only developer, after a few years you probably will not remember what the code does. If the code looks ugly, then it will take some time to research what it does, and what the logic is. However, if the code is written beautifully, then it will be easily to understand. What makes code beautiful? 1. Easily understandable variable names. 2. Consistent spacing. 3. Consistent opening and closing of if statements and loop blocks. 4. Use of functions to separate code blocks. Even so, it is likely possible to write code without functions, it will make sense to separate this logic into a separate block.

Making web pages dynamic with the use of backend

It is possible to make web pages dynamic by pulling data from a database. For example it can be a single product page, however it will pull information from a database, therefore it will make a web page dynamic. SQL stands for Structured Query Language is used to pull data from a database.

Evolution of PHP language

PHP language was first developed in 1980s. It went through several development cycles since then. What we use now as PHP is different from original PHP. There was a need to perform web development that was back-end and it was easy to use. At that time was C programming language, even so it was possible to use it to develop backend processes, however it was not simple.

History of PHP programming language

PHP programming language was developed in 1980s by Ramus Lerdorf. PHP language adds some dynamic to HTML pages. Some content in PHP is just straight HTML, and some is dynamic content by PHP.

C and C++ languages syntax

C and C++ are foundation for many languages that we use today. For example C language is using curly braces to identify pieces of code, the same principle is used in Perl.

Apache web server

Apache web server is a popular web server on the web. Apache has a root in a word “patch”, because when it was developing it was patching a lot.

Binary system

Binary system is a way computers encode information. It consists of 0s and 1s. 0 represents no signal and 1 represents availability of a signal. Computers rely on processor or CPU, processor consists of billions of transistors in a modern personal computer. You can think of a CPU as a brain of a computer. 

Computer science is a problem solving

It is possible to compare computational thinking with problem solving. You can think of a sudoku game as an example of a problem to solve. Computational thinking find a way to solve one problem and apply the same principle to solve other similar problems. It will be a bad idea to spend days just to solve a single problem and then create a solution for another one, another one and so on. Some problems will require a different solution, however if these are similar to each other, then why to create a computational solution for each one of them separately. Having one solution for them all will be more efficient and more comprehensive. Of course these problems need to be related to each other. If they are not related, then computational solutions will be very complex, and maybe it is a good idea to create multiple computational solutions that will handle things from each area separately. Please think of word processors and databases. Database engines are not meant to do word processing, so...

What is computer science?

The question may arise of what is computer science. Computer science is how to use computers to solve problems, how to calculate things. Programming is a big part of computer science, but it is not the only one out there. There is database administration, systems administration, networking, security. I probably forgot to name a few more. But you can see that computer science is more than just programming. Maybe one of these areas will be an interest to you.

Host based and network based data loss prevention systems

Data loss prevention systems analyze hosts or network for sensitive data and they will try to prevent the action from happening. Host prevention system analyze hosts for sensitive data, these help to identify systems where such information is present. Further decision needs to be made, if the data needs to be there, and who needs to have access to it. Network based data loss prevention systems will analyze traffic that leaves premises of the local network. These will also scan for sensitive data. If such data is transmitted, than appropriate actions may need to be taken.

Data at rest protection

There a few ways to prevent a loss or theft of data that is stored. Theft of the data can be prevented by implementing encryption of the data. Even if an intruder is able to access the data, it will not make sense. Backup solution means that a version of data is available in the backup. It is important to have proper data included in the backup, test the backups and store backup in a secure offsite location, since data in backups maybe sensitive. Backups need to be tested regularly to ensure that they work. Redundancy at hardware level means that multiple copies of data is made and it is located on more than one server. Snapshots allow a user to go back to a version of a file that was available on certain day. It is possible to implement redundancy of the data for real-time access to it. It can be done by having data stored in multiple servers (ideally locations) or disks.

Data in motion protection

Data in motion represents data that is moving across a wire or in wireless network. This data may need to be properly protected. If a user accesses just a blog, then it is not a big deal if others know which blog that user accesses. However if it is a checkout page, banking, or a form where a user needs to provide a password or payment information then this web page contains very sensitive information and needs to be properly secured. One of the ways to protect information from eavesdropping is to encrypt it. A web page will have a lock icon that represents that information on that web page is secure. Securing information comes at a cost of higher CPU utilization, because this content must be encrypted.

Source control

Source control allows a developer to go back to previous version of a file. In source control not only a current version of a file is saved, but the history of file changes is saved too. It is possible to go back or retrieve that version of a file.  Along with file changes it is possible to submit comments about the submission. It can be a bug fix, or some new functionality. Comments will explain of what is going on. There are number of source control applications there. A popular one is Git. It is possible to label to which source control branch files will belong to. By doing so it is possible to distinguish between which version of application will receive the update. A company may decide for how long software will be supported. After that cut off period it is unlikely that software will receive any updates.

Data protection

Data can exist in three states. These states are: at rest, in motion, in processing. I will cover in detail each one of these states, however it is important to know these three states of the data before I describe in detail each one of these.

Compensating security control

Compensating security control is a temporary measure to secure the information. For example businesses critical software may only run on an outdated version of an operating system. Compensating security control means to place the computer with that software on an isolated computer or a network segment that only a few people can access. I want to point out that it is just a temporary solution. A replacement that have good support needs to be found.

Data at rest

Data at rest represents data that is stored in some sort of medium. Data at rest may represent data that is stored in databases. It is a good idea to apply security mechanisms to sensitive data. It is a good idea to encrypt or hash this data. For example instead of storing a password hash of that password is stored, to compare the password that is stored with a password that user enters, hash of the entered password is taken, and then both hashes are compared. If hashes equal to each other, then it is possible to conclude that passwords are also equal. Two different words can result in same hash. It is called a hash collision.

Physical security controls

Physical security controls are those controls that impact physical world. Examples of physical controls are fences, alarms, locks.

Deterrent security controls

Deterrent security measures are the measures that reduce likelihood of security event from happening. Guard dogs or a fence with barb wire are examples of deterrent security measures.

Corrective security control

Corrective security control is a way to correct the situation after an incident had already occurred. Restoring data from backup and going back to a known good state is an example of corrective security control.

Detective security controls

The word detective reminds us of a person who does an investigation. Detective security controls help us to find out the root cause of the security event, Intrusion detection system is a good example of investigative security control. Even if intrusion detection system may not prevent a security incident from happening, the logs may tell where the traffic came from and which actions were taken. These logs can help security professionals or systems administrators to take appropriate actions to prevent this issue from happening again.

Preventive security control

Preventive security controls have a goal to prevent bad things from happening. Example of preventing measures are firewalls and encryption.

Security controls

To address security risks there several types of controls. I will list security controls separately and I will give short description to each one of them.

Risks often cross multiple categories

If a valuable information is stolen than it may represent strategic risk at the same time it is a reputation risk of this information of being exposed and a company receiving bad publicity.

Managerial security controls

Managerial security controls is incorporation of security practices in an organization. These may include incorporation of security practices in an organization, or incorporation of security practices into projects.

Operational security controls

Operational security controls are related to day to day activities. Log monitoring and vulnerability scanning are examples of operational security controls. It is possible to automate some of these processes to reduce volume of operational work. Operational work can be tiring not in terms of difficulty, but in terms of doing the same thing over and over again.

Technical security controls

Technical security controls enforce confidentiality, integrity and availability of information. Technical security controls include such things as firewall rules, access controls, firewalls, IPS systems.

Security controls

To respond to each security risk there is a specific security control. Security control is a measure to take to prevent bad things from happening, or if they happen than minimize the impact.

Compliance risk

Compliance risk is a risk that information that needs to be properly protected is stolen or exposed. For example personal identifying information if stolen is an example of compliance risk.

Operational risk

Strategic risk and operational risk are closely related to each other. You can think of operational risk is an event that causes a disruption of normal day to day activities of an organization.

Strategic risk

Strategic risk is risk if loosing very valuable information such as plans for a new product. For example if plans for a new product are stored in a single laptop, then a loss of this laptop will represent a big damage to a company. Proper backup or availability in multiple locations of this valuable information must be in place.

Identity theft

A very high risk of a security incident is an identity theft. Identity theft is a theft of personal identifying information such as banking information, credit card numbers, driver license information. Since theft of this information represents a very high risk, it must be properly secured.

Reputation risk

If a company went through security incident then this company may experience reputation risk. Reputation risk is a possibility of a bad publicity about that company.

A bit

One bit is the smallest amount of data. It is represented by 0 or 1. The word bit is an abbreviation of words binary digits .

Conditionals in PHP (if statements)

Conditional in PHP language are expressed with if statements. If statements evaluate if the condition evaluates to true, in this case the code which is within if statement clause will execute. If statement can end, or it may have additional conditions. Each condition maybe complex and include multiple conditions that are linked with Boolean expressions such as AND or OR. An expression can be reversed by utilizing NOT logic. If a condition evaluates to false, then code section within it does not execute.  It is possible to chain multiple conditions. These are optional. Also an optional statement is else section. The code within that section will execute if all the conditions above it will evaluate to False.

Obtain python programming language

If python is not come preinstalled with your system, then it is possible to obtain official distribution of it from python.org web site. Many of the Linux distribution allow to install version of Python, that had been tested with that version of Linux distribution. It is possible to check if python had been installed in the system by running python --version command. If python had been properly installed, then this command will give version number of python language that had been installed. Please be aware that path environment variable needs to point to location of python executable in order for this command to be successful.

Python programming language

Python is a popular programming language, it is used in many different scenarios: 1. It is possible to use for web development. 2. It is possible to use it in data science. 3. It is possible to use it for machine learning. 4. It is possible to use it for automation. 5. Software testing. 6. Scientific computing. 7. In Internet of Things devices. Python is an interpreted programming language meaning it does not compile into machine code, but requires Python executable to run code. If Python code requires libraries to execute, those need to be installed as well. Hopefully they come with the program that relies on these.

Parsing a string in PHP using regular expressions

It is possible to use preg_match function to find if a substring is in a string. This method takes three arguments: First one is a pattern. This needs to be a regular expression. I will not cover regular expression here, may be I will cover later. Second parameter is a string to search in. The third parameter is the matches. This function will return either true if a match was found, or it will return false if it wasn't. If a match was found, then third parameter of this function will be populated with the result of what had been found. Studying Preg_Match for Substring Detection Quiz: What is the primary purpose of the preg_match function as described in the source? How many arguments does the preg_match function take? What type of input is required for the first argument of preg_match? What does the second argument of preg_match represent? What happens to the third argument of preg_match if a match is found? What data type is returned by the preg_match function? When does the pre...

Finding of all occurrences of a substring in a string

It is possible to find all of the occurrences of a substring within a string using strpos method. Third argument in strpos method is the offset of the initial string. This parameter is optional, by default a substring will be searched in a string from its beginning. To find all the occurences of a substring, it is possible to make a loop, searching for a substring in different starting locations of a source string.

Searching for a substring in a string in PHP

It is possible to search if a substring exists in a string using PHP's strpos method. This method will return either numerical position where a substring starts with a search string. Starting number is 0. Or it will return false, if a string is not found.

PHP's session

PHP's session allows to store vital browser information visiting a web site in a browser's session. This is done in PHP using session_start() method. Each page that relies on session information need to use this functionality, therefore it is a good idea to handle the session by a separate file that is referred by every web site page that relies on session data. Session information is stored in browser memory therefore it can be considered secure. What information can be stored in session? - Web site user name. - Web site login state. Information is written to a session using keyword $_SESSION, followed by name of the property in square brackets. This name of session variable also must be included in quotes. This must be followed by equal sign and the data that needs to be stored in session.  Once session data is written, session needs to be closed by session_write_close() statement. Session data can be retrieved by using keyword $_SESSION that follows name of a session varia...

Namespaces in PHP

It is possible to create a namespace in PHP. It is done by using namespace keyword followed by the name of the namespace. Why namespace is needed? If multiple developers work on the same project, than it is possible to have class names collisions, different developers may name classes by the same name. To prevent this collision issue from happening, each developer will create classes in its own namespace. Even so class names may be the same, they will be treated as different classes because they belong to different namespaces.

Methods and property visibility in PHP

It is possible to have three types of visibility of PHP properties or methods in classes. These are public, protected and private.  The difference between these three as follow: Public properties and methods are accessible by anyone. Protected are accessible by this class and classes that extends it. Private properties are accessible by this class only. YouTube video FAQs What are the three visibility types for properties and methods in PHP classes? The three visibility types in PHP for class properties and methods are public, protected, and private. How does public visibility affect access? Properties and methods marked as public are accessible from anywhere, both inside and outside the class where they are defined, and also in any classes that extend the original class. What is the scope of protected visibility? Protected properties and methods are accessible within the class where they are defined and within any classes that inherit from or extend that class. They are not access...

Advantages of Object Oriented Programming (OOP)

At a first glance OOP development just makes development effort just more difficult, same function can be used in procedural language. OOP development makes development of complex projects easier. Even so OOP requires change of mindset from procedural language language. Most of the modern programming languages are OOP languages. Example of non-OOP language is assembly language.

Parsing a string in PHP using regular expressions

It is possible to parse a string in PHP using regular expressions. This is done using preg_match function. This function takes three arguments. First one is regular expression pattern, second one is a string where a match is to be looked for, and the third argument is a variable to hold the result of this operation. If this operation is successful, then the third argument will be populated with matches of the regular expression. Result of this function is a count of how many matches were found. Ig no matches were found then the result of this operation will be 0, or in terms of Boolean values, it is False.

Searching if a string exists in another string in PHP

It is possible to use strpos PHP method to investigate if a string exists in another string. This method takes two arguments. First one is a string to search in, second argument is a string to search for. This method will return either a numeric value, if a substring exists in another string. Please note that the return value is zero based. First element will have a count of zero. If substring is not found in a string, than this method will return false. Please be careful when checking value 0. As value 0 can be treated as false.

Getting a substring of a string in PHP

It is possible to use substr PHP method to get a substring of a string. This method can take either two or three parameters. First parameter is a variable that contains a string. Second parameter is from which character it is necessary to start getting a substring, The count starts with 0. First character in a string is element 0. Second argument is number of characters to take from this string. If second argument is not specified, then substr method will take a substring until the end of a string supplied. PHP substr Method Study Guide Quiz What is the primary function of the substr method in PHP? How many parameters can the substr method accept? What data type should the first parameter of the substr method be? What does the second parameter of the substr method represent? What is the starting index for the characters in a string when using substr? What does the third parameter of the substr method represent? What happens if the third parameter is not provided? If you want to extract...

Converting a string to an array in PHP

It is possible to convert a string to an array in PHP, this is done using explode function. This function takes three arguments. Two arguments are mandatory and the third one is optional. First argument is a character that identifies the character that is between elements of this string that will become array elements. Second argument is the name of the array. Third argument is optional. By default a string will be converted to a single array. If the value of this argument is specified, then array will contain as many elements as specified, The last array element will contain the rest of the string.  If the last argument is negative, then all parts of the string will be converted to an array except as many elements as specified.

Extracting a substring in PHP

It is possible to extract a substring in PHP by two different methods. Method one is to use substr method. This method takes three arguments. First one is a variable name, second one is starting number, third argument is number of characters that need to be extracted. Second one is to treat a string as an array of characters.

Multiline text in PHP

There are couple of ways of specifying multiline text in PHP. It is possible to use \n characters to specify a new line, or it is possible to specify a multiline text in PHP. This done using three less than signs followed by specific characters. End of that text will be represented by repeating the same characters again. Anything that in between those characters will be treated as text.

Single and double quotes in PHP

It is possible to include a variable inside of a text that is surrounded by double quotes, however if that variable is used inside of a text, that is surrounded by single quotes, it will not be treated as a variable, but as a text.

Pure functions in PHP

Pure functions in PHP do not output anything to a screen.

Anonymous functions in PHP

Anonymous function in PHP have two things: 1. They don't have a name. 2. They are assigned to a variable. Variable in this case will have a calling parameter.

Function's scope in PHP

Variables that are inside of PHP function have local scope. It means two things: 1. It is possible to have a variable with the same name as in the scope of the main code. 2. Modification of that variable value will not impact the variable that is declared in the main section of the code.

Passing parameters by reference to a PHP function

It is possible to pass a variable by reference to a PHP function. Passing by reference is specified by ampersand sign in front of function's variable. If a variable is passed by reference, then value of that variable will be modified. Objects always are passed to a PHP function by reference, there is no need to specify ampersand sign.

Optional function parameters in PHP

It is possible to have optional function parameters in PHP. Default value of that parameter will be specified in function declaration using equal sign. If a different value is supplied to a function, that value will be used instead.

Scope of variables

Scope of variables can vary. There could be a global variable that is accessed everywhere, however it is a bad idea to use global variables in methods. Data to methods should be passed as parameter values of that method. Inadvertent modification of that variable value inside of that method may have big consequences. Instead of using global variables in methods, data to the methods needs to be passed as parameters of that method. Study guide 1. What is variable scope in programming? Variable scope refers to the region in a program where a variable can be accessed and used. Some variables might be accessible throughout the entire program, while others might only be available within specific blocks of code or functions. 2. What is a global variable? A global variable is a variable that is declared outside of any function or block of code and can be accessed from anywhere within the program. 3. Is it a good practice to use global variables within methods? No, it is generally considered a b...

Optional function parameters in PHP

PHP's functions can have optional parameters. Default value of that variable will be specified after the equal sign. If a value is not passed to that function, than default value will be used, if a value is passed then this value will be used instead the default one.

while loop in PHP

Code inside of while loop in PHP will be executed as long as condition of while loop is true. Please don't forget to change the variable inside of this loop so the condition will eventually will be equal to false. If that variable is not adjusted, then there is a risk of entering infinite loop.

for loop in PHP

for loop is useful in a case when it is known how many times this loop will iterate or repeat execution of the code inside of it. for loop has three elements: initial value of a variable, final value of that variable, and how the variable needs to be adjusted with each iteration of that loop. Common methods to increase or decrease the value of a variable by one.

do while PHP loop

do while loop will execute until the condition of this loop is true. Please don't forget to adjust the value of the variable inside this loop that is used in the condition of that loop. If the value of that variable is not adjusted then there is a risk of entering infinite loop, where the code inside the loop will repeat.

foreach PHP statement

If a list of items is given, then foreach statement will iterate over each element of the array. foreach statement in a loop is useful where number of elements in the array is not hardcoded and can be changed.

break statement in PHP

Just like continue statement in PHP will alter execution of a loop, break statement will alter execution of a loop too. If continue statement will start execution of a loop from the beginning, than break statement will exit execution of a loop.

continue statement in PHP

continue statement in PHP will cause a loop to start from the beginning. Even so there could be other code in a loop, it will not be executed.

switch statement in PHP

It is possible to simplify logic of the PHP code with switch statement rather then multiple conditions in an if statement. Conditions in an  if statement may become cumbersome and the logic of that if statement maybe unclear. switch PHP statement simplifies that logic. Even so it is possible to have multiple conditions in switch statement that will evaluate to True, only the first one section of the code that follows the condition that evaluated to True will be executed. It is possible to have default statement in a switch statement. Code that follows default statement will be executed in a case when all other conditions in a switch statement evaluate to false.

Returning data from a function

Returning data from a function in PHP is done using return keyword that follows a value or a variable that contains the data that needs to be returned by this function. The data which is returned by the function needs to match data type of a variable. For example it makes no sense to assign a string data type, when is numeric value is expected from a function.

Including other file in PHP for execution

In PHP there are two statements that will include functionality from other file. These keywords are include and require . Even so both of these statements will make code available for execution that is located in an another file, functionality of these two statements differ. If a file that needs to be included is absent, than execution of code that follows include statement will be performed, however execution of the code will be aborted, if a file to which require keyword points to is absent. Splitting functionality onto different files is useful in three cases: 1. Bringing functionality from another developer. 2. Making code more clearer to understand. 3. Avoid errors. If a code is included is known good code and a error arises, than error is probably is not within the code that is included.

goto PHP operator

goto operator will skip execution of code and continue execution of a code that follows a label to which goto operator points to. If code has many goto operators, than this code probably needs a revision. It is possible to write code that executes without goto operators.

Loops

Loops are repetition of the same code several times. Loops are needed to simplify code. Even so it is possible to repeat the same code several times, it is probably not a good idea. Code will be executed inside of a loop until the loop's condition is true. It is possible to perform execution of the code inside of a loop before the loop's check is performed, or after that check is performed. This is functionality of PHP, this functionality cannot be blindly be applied to other programming languages, as implementation of a loop functionality may differ. Inside of a loop it is needed to adjust the value of a variable that is included in the check. Typically this variable is adjusted by one. By doing so it is possible to tell how many iteration of this loop will be. If a variable that is included in a loop check is not adjusted, then this loop can perform executions over and over again. It is known as indefinite loop.