C++ for PHP Developers
Transfer PHP experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.
PHP and C++ side by side
The same program in PHP and in C++, from the lessons of this course.
Strings and Output
A PHP string is already a mutable byte sequence with value semantics: assigning it copies it, $s[0] = "O" changes one byte, and strlen counts bytes, not characters (that is mb_strlen). C++ std::string works the same way underneath: it owns its bytes, a copy…
PHP1<?php2$name = "oak";3$copy = $name;4$copy[0] = "O";5$copy .= "s";6echo $name, " ", $copy, " ", strlen($copy), "\n";C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";15name[0]='O';16name+="s";17std::string label=name+" x"+std::to_string(2);18std::cout<<label<<" "<<label.size()<<"\n";19}Text Equality
PHP == is a loose comparison that juggles types: two numeric strings are compared as numbers, so "1e3" == "1000" is true. === compares type and value, and on objects it asks whether both variables refer to the same instance. C++ operator behavior depends on…
PHP1<?php2$a = "1e3";3$b = "1000";4var_dump($a == $b);5var_dump($a === $b);6var_dump("red" === "red");C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="1e3",b="1000",c="red";15std::cout<<std::boolalpha<<(a==b)<<" "<<(c=="red")<<"\n";16}Fixed-size Arrays
A PHP array is an ordered map that grows on demand with $items[] = 12, and assigning it copies it (copy-on-write). Negative indexes do not count from the end: $items[-1] is just a missing key. C++ std::array<T,N> is a fixed-size value type. Its size is part…
PHP1<?php2$items = [4, 8];3$items[] = 12;4$copy = $items;5$copy[0] = 99;6echo count($items), " ", $items[0], " ", $items[count($items) - 1], "\n";7echo implode(",", array_slice($items, 0, 2)), "\n";C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};15std::array<int,3> b=a;16b[0]=99;17std::cout<<a.size()<<" "<<a[0]<<" "<<a.at(1)<<"\n";18}Value Copies
PHP arrays are values: after $b = $a, $b behaves as its own array (PHP shares the storage until the first write, then copies), so appending to one never changes the other. Objects are different: a variable holds a handle, so copying an array of objects copies…
PHP1<?php2$a = [4];3$b = $a;4$b[] = 9;5echo count($a), " ", count($b), "\n";6$item = new stdClass();7$item->qty = 1;8$list = [$item];9$copy = $list;10$copy[0]->qty = 5;11echo $list[0]->qty, "\n";C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<int> a{4};auto b=a;b.push_back(9);std::cout<<a.size()<<" "<<b.size()<<"\n";15std::vector<std::vector<int>> grid{{1}};auto g=grid;g[0].push_back(2);std::cout<<grid[0].size()<<"\n";16}Functions and Returns
PHP checks a function's type declarations at run time, on each call. Without declare(strict_types=1), a scalar argument is coerced: the string "4" passed to an int parameter becomes 4. A PHP function that ends without return gives null. C++ function…
PHP1<?php2function twice(int $value): int {3 return $value * 2;4}5function shout(string $s) {6 strtoupper($s);7}8echo twice("4"), "\n";9var_dump(shout("hi"));C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int triple(int n){return 3*n;}14void report(int n){std::cout<<"value "<<n<<"\n";}15int main(){16report(triple(4));17}Streams and Exact Parsing
PHP casts are forgiving: (int) "7x" reads the leading digits and gives 7 without a warning, and (int) "oak" gives 0. To validate, use filter_var($s, FILTER_VALIDATE_INT), which returns false unless the text is an integer. C++ streams represent extraction…
PHP1<?php2[$word, $number] = explode(" ", "oak 7x");3echo $word, "/", (int) $number, "\n";4var_dump(filter_var($number, FILTER_VALIDATE_INT));C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7x");std::string word;int n;std::string rest;15if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";16if(in>>rest)std::cout<<"trailing "<<rest<<"\n";17}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsC++ Files and Values
6 lessonsConditions and Iteration
5 lessonsStandard Containers
5 lessonsCopies and Algorithms
5 lessonsFunctions and Templates
5 lessonsParsing and Lifetime
5 lessonsObjects and Invariants
6 lessonsComposition and Ownership
5 lessonsProject: Library Desk
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is C++ for PHP Developers for?
For people who already write PHP and want to learn C++. Each lesson starts from PHP you know and shows how C++ does the same thing, so you skip the beginner basics.
Do I need to know C++ already?
No. The course starts with your first C++ program. You need PHP basics: variables, conditions, loops, functions and collections.
How many lessons does C++ for PHP Developers have?
C++ for PHP Developers contains 48 interactive lessons you can complete at your own pace.
What does C++ for PHP Developers cover?
The course is organized into chapters covering: C++ Files and Values, Conditions and Iteration, Standard Containers, Copies and Algorithms, and more.
Is C++ 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.