Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Friday, November 22, 2024

Top 8 Types of Functions in PHP You Must Know

PHP is a powerful server-side scripting language offering a wide range of functions to facilitate various tasks. Functions in PHP are blocks of code that execute only when called. They can accept data, parameters as input, process it, and return an output using 'return' or simply execute code within the function without returning a value. Such functions are called void functions. If you use a function that PHP has already written for you, such as 'is_numeric' and many others, these are called built-in functions. If a function is located within a class, it is called a method. Regardless of whether you are working with built-in functions, writing your own, or using advanced concepts like closures, understanding functions is crucial for efficient PHP programming. Functions in PHP are a powerful tool that enables modularity, code reuse, and flexibility. You should use them whenever it makes sense in your code, while also professionally creating your own.

Many years ago, if you asked us how many types of functions there are, we would have thought about the basic division and answered four:

a function that takes no parameters and returns a value

a function that takes parameters and does not return a value

a function that takes no parameters but returns a value

a function that takes no parameters and do not return a value

Today, PHP has proven us wrong. There are many more types of functions in the PHP programming language, and today we will list the top 8 that you will often use in your projects.

The student is thrilled with exploring the capabilities of functions in PHP

The student is thrilled with exploring the capabilities of functions in PHP

To make things much clearer for you regarding the numerous functions in PHP, let's dive straight into the practical part when it comes to functions. This is the easiest way to go through and explain even 8 types of functions in the PHP programming language. Unlike previous lessons in this PHP tutorial, today we'll expand our PHP coding by using a CSS file in addition to Bootstrap. You might wonder why use CSS when you're using Bootstrap. The reason is simple. There are things in HTML and PHP coding that you can't always style using Bootstrap alone. So, for larger projects, you'll often find that CSS styling is used in addition to Bootstrap. Nowadays, there are systems that don't allow the use of Bootstrap for security reasons, they only use CSS, while we've never seen a project that prohibited the use of CSS. That's why you should always learn and know how to use HTML and CSS together with PHP. Our HTML & CSS tutorial will make it easier for you to learn or simply refresh your memory, click here.

Then we'll learn how to pass data from one PHP page with an HTML form to another PHP page for further processing. Also, since there are multiple functions, we'll place them in a separate PHP file and teach you how to connect PHP files, so that you have the impression that separate functions in another PHP file are available as if they were in the same one. This PHP lesson might sound a bit complicated, but let us reassure you by coding everything together, writing simple code that you can definitely use in your projects.

Choose the Right Function for the Job: 8 Function Types for Better Code

Sunday, September 29, 2024

Understanding PHP Data Types, A Practical Guide

Even though we have already installed and prepared everything we need to learn and program a programming language, as we prepared to learn the PHP programming language in the previous PHP tutorial post, see here; and since we have printed the famous "Hello World" on the local server web page; the first steps in programming begin with variables. Variables in programming are places in computer memory where data is stored that the program uses for calculation, processing, and manipulation. In the PHP programming language, as in other programming languages; variables are names used to reference places in memory. The easiest way to understand variables is to think of them as memory boxes. But not all boxes are the same. In some, you can put a value that represents an integer, while in others you can put floating-point numbers. There are also boxes in which you can put a set of any characters. To know what value, you can put in a variable, you need to know what data type you have decided to assign to a variable. All programming languages have variables and certain data types.

Most basic variable types are essentially the same or similar. However, some programming languages have more data types while others have fewer. Also, even the same name of a variable type in many programming languages can have different limitations. For example, in the PHP programming language, the float data type has the precision of the double data type, which means that the value of floating-point numbers is much larger than it would be if the data type was float in the C# programming language. Or instead of the char data type in C#, the string type is used in the PHP programming language, while char is a function used to convert an ASCII number to a character. So that you are not confused at the very beginning of learning data types, it is best to concentrate on the data types that the PHP programming language has.

The boss explains the importance of data types in PHP to a new employee

The boss explains the importance of data types in PHP to a new employee

Variables are the fundamental building blocks of every program. They serve as containers for data that can be changed and manipulated throughout the program's execution. In PHP, variables are declared simply by assigning a value to a name. The data type of a variable is determined automatically based on the assigned value. Variable names can include letters, numbers, and underscores, but they must start with a letter or an underscore. Additionally, PHP requires a dollar sign ($) before each variable name to identify it. For example, in this line, we've created a variable named $number and assigned the integer value 10 to it.

$number = 10;

It's essential to choose clear and descriptive variable names to enhance code readability. For instance, if a variable holds the value of the 13th salary, a suitable name would be $thirteenthSalary. Remember that PHP is case-sensitive, meaning $number and $Number are considered different variables. A common convention in PHP is to use camelCase for variable names, as seen in $thirteenthSalary.
Unlike many other programming languages, PHP doesn't require explicit type declarations. When you assign a value to a variable, its data type is inferred automatically, and the necessary memory is allocated.

Basic Data Types in PHP: Simply Explained

Monday, February 08, 2016

Statičke klase, metode, konstruktori i promenjive u C# programskom jeziku



Kao što bi to Microsoft programeri rekli, statička klasa vam je u osnovi isto što i klasa koja nije statička ali sa tom razlikom što se statička klasa ne može instancirati. Drugim rečima, ne možete koristiti ključnu reč new za stvaranje promenjive tipa klase. Prva stvar koju treba da znate o statičkim klasama je da ih nikad ne pravite iako samo pretpostavljate da će se klasa nasleđivati. Statičke klase ne možete nasleđivati i jednostavno se podrazumeva da je svaka statička klasa i sealed klasa i bez korišćenja ključne reči sealed. Znači statičku klasu pravite samo ako ste sigurni da se nikad klasa koju pravite neće nasleđivati.  


( Statički članovi štede memoriju prilikom svake inicijalizacije klase )


Statičke klase se jednostavno prave umetanjem ključne reči static ispred ključne reči class prilikom pravljenja klase, ali i svaka statična klasa mora da sadrži samo statičke članove. U Visual Basic-u se umesto static koristi ključna reč Shared i često će te čuti Visual Basic programere da statičku klasu zovu deljena klasa ili statičku metodu zovu deljena metoda, što statičke klase i jesu. Samo to nemojte da mešate sa partial klasama koje dele klasu u više fajlova. Kad kažu za statičku klase da je deljena klasa misli se na deljene memorijski resursa jer statički članovi štede memoriju. Pogledajte inicijalizaciju klase koja nije statična kroz objekat klase.

Rectangle rectangle = new Rectangle();
WriteLine(rectangle.CalculateArea(5, 7));
WriteLine(rectangle.CalculateArea(8, 9));

Vi pozivate metodu CalculateArea preko objekta. Međutim dovoljno je samo metodu napraviti statičnu i jednostavno je pozivate preko klase.

Rectangle rectangle = new Rectangle();
WriteLine(Rectangle.CalculateArea(5, 7));
WriteLine(Rectangle.CalculateArea(8, 9));

Ukoliko je klasa statična, onda ne morate ni praviti inicijalizaciju objekta.

WriteLine(Rectangle.CalculateArea(5, 7));
WriteLine(Rectangle.CalculateArea(8, 9));

Ako imenujete namespace kao statičan, npr:

using static StaticClass.Rectangle; // StaticClass je ime programa

Tada možete pisati još kraće kod.

WriteLine(CalculateArea(5, 7));
WriteLine(CalculateArea(8, 9));

Razumevanje statičkog konstruktora