3

How To Write Variables in Different Programming Languages

 1 year ago
source link: https://erisanal.com/how-to-write-variables-in-different-programming-languages/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hey guys

In today’s lesson, we will walk through how to write variables in 7 different programming languages. Since variables in programming are indispensable when coding.

What Are Variable in Programming?

Variables are just a way to save data or information for usable purposes. Variable makes a program to be more clear to read by the coder

and others. All programming languages have their own way to write and execute a  variable.

Nevertheless, am going to mention how to write a variable in PHP, JavaScript, Python, Java, C#, C++, and CSS.

1. How To Write a Variable in JavaScript

JavaScript accepts a string, integer, float, Boolean, etc. to be stored in a variable. Variable can be declared in JavaScript using let , constand var. The const is sometimes referred to as constant in JavaScript.

Let’s Do Some Example

JavaScript Variables

Using the var , let and const to declare different type of Data types in JavaScript Respectively.


var strg= "Erisanal.com" 
const num = 100 
let bolln = true 

console.log(strg) //Output Erisanal.com
console.log(num) //Output 100
console.log(bolln) //Output true

2. How To Write a Variable in PHP

To declare a variable in PHP, you must start with the $ sign followed by the name of the variable, the equal to sign =is used to assign value to the variable name.

Let’s do an example

PHP Variable

<?php
// Declaring variables
$txt = "Hello World!";
$num = 100;
 
// Displaying variables value
echo $txt;  // Output: Hello World!
echo $num; // Output: 100;

?>

2. How to declare a constant variable in PHP

Unlike JavaScript, the const keyword is used to define a constant variable, but in PHP is completely different. Thedefine( ) function is used to define a varibale in PHP whick takes three parameters as an argument.

Syntax

define(name, value, case-insensitive)

PHP define( ) Parameter Explained

  • name: This is the first parameter that serves as the name of your variable
  • value: This is the data you want to store in the variable, this can be an integer, boolean strings, or any PHP data types.
  • case-insensitive: This is optional, this is used to enable and disable case-sensitive and case-insensitive respectively. This parameter only accepts a boolean value (i.e true or false). The default is false which means the variable is case-insensitive. To enable case-sensitive change to true.

Let’s create a PHP constant with a case-sensitive name:

Example One.

<?php
define("Name", "Erisanal.com", true);
echo Name;
?>

Let’s create a PHP constant with a case-insensitive name:

Example Two

<?php
define("Name", "Erisanal.com", false);
echo Name;
?>

// The above code can also be written as:

<?php
define("Name", "Erisanal.com");
echo Name;
?>

3. How To Write a Variable in Python

Since Python is completely object-oriented, and not “statically typed”, it doesn’t have a command you can use to declare a variable. A python variable is declared once you have added value to it.

To assign a value to a python variable, use the equal to = sign


strg= "Erisanal.com" 
num = 100 

print(strg) #Output Erisanal.com
print(num) #Output 100

How to declare a constant variable in Python

In python, there is no support for constant variables, hence constant variables can’t be declared in python.

4. & 5. How To Write a Variable in C++ and C#

Declaring a variable in C++ and C# is strict.  both languages executes it’s variable in almost the same way.

To declare a variable in C++, and C#, the type must be specified before you assign a value to it. There are different type of variable in C++ and C#, some of these are:

  • String – Are text that is surrounded by a double quote e.g “Hello World !”
  • int – This is is an integer, sometimes referred to as whole numbers. Numbers without decimal such as 987, -987
  • double – is a decimal number e.g 987.00 or -987.00
  • char – stores single characters that are surrounded by a single quote e.g ‘Z’ or ‘a’
  • bool – stores boolean value such as true and false only

Declaring a Variable in C++, and C#

Generally, All programming languages abide to using the equal to sign = to declare a variable.

Syntax

type variable = value;

Now, let’s move by creating different types of variable

C++, and C# Variable

String myWebsite = "Erisanal.com";
char myLetter = 'E';
int myNum = 905;
float myFloatNum = 2.99f;
boolean isBool = false;

6. How to declare a Variable in Java

To declare a variable is almost the same as to declare a C++ and C# variable, All these programming languages require the data type to be specified before you assign a value to it.

The difference lies in the variable type. In Java, this is how the variable type can be declared.

  • String – Are text that is surrounded by a double quote e.g “Hello World !”
  • int – This is is an integer, sometimes referred to as a whole number. Numbers without decimals such as 987, -987
  • float – is a decimal number e.g 987.00 or -987.00
  • char – stores single characters that are surrounded by a single quote e.g ‘Z’ or ‘a’
  • boolean – stores boolean values such as true and false only

Declaring Java Variable

Example

int myNum;
myNum = 15;
System.out.println(myNum); //Output 15

7. How To Write a Variable in CSS

CSS also has a way it executes a variable. To declare a CSS variable is different from other programming languages variable. In most programming languages, the equal to sign is used to assign a value to the variable while in CSS is entirely unlike.

How to Declare a CSS Variable

The column : sign is used to a assign value instead. The CSS variable is usually defined within the CSS :root { } pseudo-class.

A CSS variable usually starts from -- followed by the variable name. The var ( ) function is used to call and execute a CSS variable

Let’s quickly do an example

CSS Variable

:root {
--color : #3e5afb
}

/* Calling a CSS variable

body { 
color: var(--color)
}

Conclusion

These are how variables is been declared in the programming languages mentioned. Hope you enjoyed this post, Share your thought by commenting.

Tags: tech

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK