Example 2: Personal Data
Concatenating multiple variables in PHP
Download file datos_personales🎯 Live Demo
Code output:
Personalize your data!
📖 Code Explanation
Complete PHP Code
<?php
$nombre = "Jorge";
$edad = "21";
$ciudad = "Talavera de La Reina";
echo "Hola, me llamo $nombre, tengo $edad años y soy de $ciudad";
?>
📝 Lines 1-3: Variable declaration
$nombre = "Jorge";$edad = "21";$ciudad = "Talavera de La Reina";
What does it do? Creates three different variables with personal data.
Why is it done this way? Each variable stores a specific type of information, making the code more organized and easier to maintain.
💡 Concept: Variable naming
It's good practice to use descriptive names for variables. In PHP we use snake_case (all lowercase with underscores).
📝 Line 4: Automatic concatenation
echo "Hola, me llamo $nombre, tengo $edad años y soy de $ciudad";
What does it do? Combines fixed text with variables into a single message.
Why is it done this way? PHP automatically replaces variables inside double quotes, making concatenation more readable.
💡 Concept: Concatenation vs Interpolation
Interpolation: Variables inside double quotes (more readable)
Concatenation: Join strings with . (more control)
📝 Alternative with concatenation
echo "Hola, me llamo " . $nombre . ", tengo " . $edad . " años y soy de " . $ciudad;
What does it do? The same result but using the concatenation operator .
When to use it? When you need more control or when using single quotes.
💡 Concept: Concatenation operator
The . dot joins strings. Useful when working with single quotes or needing complex formatting.
🎯 Key Concepts Learned
Multiple Variables
- You can declare as many variables as you need
- Each variable must have a unique name
- Organizes related information
- Facilitates data reuse
Variable Interpolation
- Only works in double quotes
- More readable than concatenation
- PHP replaces automatically
- Ideal for simple texts
Data Types
- String: Text in quotes
- Integer: Whole numbers
- Float: Decimal numbers
- PHP converts automatically
💪 Practical Exercise
Extend the code
Practice these changes to improve your skills:
- Add a variable
$paisand include it in the message - Change the message to use single quotes with concatenation
- Create a more formal message: "Mi nombre es [nombre], tengo [edad] años y resido en [ciudad]"
- Experiment mixing numbers as text and as integers
Solution 1: Add country
<?php
$nombre = "Jorge";
$edad = "21";
$ciudad = "Talavera";
$pais = "España";
echo "Hola, me llamo $nombre, tengo $edad años y soy de $ciudad, $pais";
?>
Solution 2: Single quotes
<?php
$nombre = "Jorge";
$edad = "21";
$ciudad = "Talavera";
echo 'Hola, me llamo ' . $nombre . ', tengo ' . $edad . ' años y soy de ' . $ciudad;
?>
Solution 3: Formal message
<?php
$nombre = "Jorge";
$edad = 21;
$ciudad = "Talavera de La Reina";
echo "Mi nombre es $nombre, tengo $edad años y resido en $ciudad";
?>
Solution 4: Data types
<?php
$nombre = "Jorge";
$edad = 21; // Como integer
$ciudad = "Talavera";
echo "Hola, me llamo $nombre, tengo " . $edad . " años y soy de $ciudad";
?>
⚖️ Comparison: Interpolation vs Concatenation
Interpolation (Double Quotes)
echo "Hola $nombre";
✅ Advantages:
- More readable
- Shorter code
- Easier to write
❌ Disadvantages:
- Only in double quotes
- Less control
- Can be confused with arrays
Concatenation (Operator .)
echo 'Hola ' . $nombre;
✅ Advantages:
- Works with any type of quotes
- More control over formatting
- Better for complex code
❌ Disadvantages:
- Longer code
- More error-prone
- Less readable