PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

PHP · Theory

Variable Scope

← All stacks

Theory

10/352

Variable Scope

A PHP variable starts with $ and a letter or underscore. $marks = 72; $name = 'Asha'; No type on the left by default. Types exist at runtime (int, string, array, object, null).

Names are case-sensitive — $Marks is not $marks. Assignment is = . Compare with == or === (=== also checks type).

Trap — marks = 72 without $. Trap: == vs = inside if ($marks = 40) which assigns.

Exam tip

$marks = 72; Names start with $.

Example

<?php
// Variable Scope
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
  if ($s >= 80) echo "high: $s\n";
}

Variable Scope: scores array, print avg, then high marks. Say the output.

Short notes

  • Def$name holds a value. Starts with $.
  • Rule= assign. === compare type+value.
  • TrapMissing $. = inside if.

Questions

1

How do you create a variable?

10 / 352

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.