Menu
CoddyTech
Course

Dart for PHP Developers

Move from PHP to standalone Dart: typed functions, null safety, collections, exceptions, classes, interfaces and mixins. Build a parcel depot across four connected steps and port two complete PHP programs.

PHP and Dart side by side

The same program in PHP and in Dart, from the lessons of this course.

  1. Output and Comments

    A PHP CLI file executes its top-level statements after the opening tag; Dart starts in main. PHP echo does not add a newline, so these examples request one explicitly, while Dart print adds it automatically. Both languages interpolate dollar-prefixed names in…

    PHP
    1<?php2$lane = 3;3echo "lane:$lane\n";
    Dart
    1void main(){final lane=3;print('lane:$lane');}
  2. Boolean Conditions

    PHP conditions coerce values to Boolean: zero, an empty array, an empty String and the String "0" are among false values. Dart requires bool for conditions and logical operands. Both && and || short-circuit, so the right operand may never run. PHP also offers…

    PHP
    1<?php2function probe(): bool { echo "probe\n"; return true; }3if (false && probe()) { echo "open\n"; }4else { echo "closed\n"; }
    Dart
    1bool probe(){print('probe');return true;}2void main(){if(false && probe()){print('open');}else{print('closed');}}
  3. Functions and Returns

    PHP can declare parameter and return types, and its fn syntax creates an anonymous function with one returned expression. Dart uses => for an expression body on a named function or closure. An untyped PHP function may fall through and return null; a Dart…

    PHP
    1<?php2$isLog = fn(string $name): bool => str_ends_with($name, '.log');3echo json_encode($isLog('server.log')), "\n";
    Dart
    1bool isLog(String name)=>name.endsWith('.log');2void main(){print(isLog('server.log'));}
  4. Iterables and Laziness

    PHP array_map eagerly calls the callback and returns an array; a function using yield can instead produce a lazy Generator. Dart map and where normally return lazy Iterables whose callbacks run as elements are requested. Calling toList materializes a Dart…

    PHP
    1<?php2$result = array_map(function (int $n): int {3    echo "visit:$n\n";4    return $n * 2;5}, [2, 3]);6echo "made\n", $result[0], "\n";
    Dart
    1void main(){final result=[2,3].map((n){print('visit:$n');return n*2;});print('made');print(result.first);}
  5. Parsing and Validation

    A PHP integer cast accepts numeric prefixes and can turn invalid text into zero, so casting alone is not validation. Here a whole-String regular expression checks integer syntax before conversion. Dart int.tryParse instead signals invalid text with null…

    PHP
    1<?php2foreach (['nope', '42'] as $text) {3    echo preg_match('/\A[+-]?[0-9]+\z/', $text) === 14        ? (string) (int) $text : 'invalid';5    echo "\n";6}
    Dart
    1void main(){print(int.tryParse('nope')??'invalid');print(int.tryParse('42'));}
  6. Classes and Instances

    PHP and Dart both declare classes with instance members, but PHP accesses them through $this-> while Dart can use this implicitly. PHP constructs an object with new; Dart constructor calls do not require that keyword. Object variables in both languages refer…

    PHP
    1<?php2class Lamp {3    public bool $on = false;4    public function toggle(): void { $this->on = !$this->on; }5}6$a = new Lamp(); $b = new Lamp();7$a->toggle();8echo json_encode($a->on), ':', json_encode($b->on), "\n";
    Dart
    1class Lamp{bool on=false;void toggle(){on=!on;}}2void main(){final a=Lamp(),b=Lamp();a.toggle();print('${a.on}:${b.on}');}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for PHP developers

Learn Dart from another language

Frequently asked questions

Who is Dart for PHP Developers for?

For people who already write PHP and want to learn Dart. Each lesson starts from PHP you know and shows how Dart does the same thing, so you skip the beginner basics.

Do I need to know Dart already?

No. The course starts with your first Dart program. You need PHP basics: variables, conditions, loops, functions and collections.

How many lessons does Dart for PHP Developers have?

Dart for PHP Developers contains 48 interactive lessons you can complete at your own pace.

What does Dart for PHP Developers cover?

The course is organized into chapters covering: First Dart Programs, Flow and Decisions, Functions and Values, Collection Pipelines, and more.

Is Dart for PHP Developers free?

Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.

Coddy programming languages illustration

Start Dart for PHP Developers for free

Get started