Showing posts with label gettype. Show all posts
Showing posts with label gettype. Show all posts

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

Tuesday, February 23, 2016

Object klasa u C# programskom jeziku


Već vam je poznato da klasu možete da posmatrate i kao šablon objekta, dok za objekat možete da kažete da je objekat instanca klase. Međutim, šta je Object klasa? U mnogim programskim jezicima, tako i u C# programskom jeziku, za sve klase postoji koreni tip iz koga se izvode svi ostali objekti u hijerarhiji. Tako su sve .NET klase u krajnjoj liniji izvedene ili preciznije rečeno nasleđene iz klase Object koja se nalazi u imenskom prostoru System (System.Object). Ukoliko u C# programskom jeziku ne naznačite da je neka klasa izvedena iz druge klase, kompajler automatski pretpostavlja da je navedena klasa izvedena iz klase Object. Što u praksi znači da vi automatski imate pristup velikom broju javnih i zaštićenih metoda članova koji su već definisani za klasu Object. Ove metode su vam takođe dostupne i u svim drugim klasama koje definišete.

 
( Metode Object klase u C# programskom jeziku )

Isto tako kada koristite u vašim deklaracijama tip object, on definitivno predstavlja krajnji roditeljski tip iz kog se dalje izvode svi izvorni i korisnički definisani tipovi. Ključna reč ili tip object je samo alias za System.Object klasu. To je najključnija stvar u C# programskom jeziku po kojoj se C# programski jezik razlikuje od C++ programskog jezika. Pre nego što pređemo na praktičan primer kako se koristi klasa Object, pogledajte spisak metoda koji su definisani u klasi Object.
  • ToString() – Vraća objekat u obliku string.
  • GetHashCode() – Koristi se prilikom implementiranja rečnika (heš tabela).
  • Equals(Object) – Poredi jednakost primerka objekta.
  • Equals(Object, Object) – Poredi jednakost dva primerka objekta.
  • ReferenceEquals(Object, Object) – Proverava da li se dve reference odnose na isti objekat.
  • GetType() – Vraća detalje tipa objekta.
  • MemberwiseClone() – Pravi površnu kopiju objekta.
  • Finalize() – Ovo je .NET verzija destruktora.
Da bi ste koristili navedene metode u vašim klasama, većinu njih treba da prekoračite sa ključnom rečju override i napišete vašu implementaciju. U praksi bi na primer svaka klasa trebala da ima svoju implementaciju metode ToString() inače svako ko koristi vašu klasu može pozvati metodu ToString() jer vaša klasa je izvedena iz Object klase; ali ako niste implementirali ovu metodu, nećete dobiti rezultat koji očekujete.

Kako u praktičnom primeru da koristimo navedene metode?