PHP & Web Development Blogs

Showing 41 to 45 of 48 blog articles.
3410 views · 2 years ago

![Type Arrays with Variadic Functions in PHP](https://images.ctfassets.net/vzl5fkwyme3u/6e5WrjNthmKmS6C2AAWoi0/889f4e4afe965d040b17fbe7834ad07a/AdobeStock_200448817.jpeg?w=1000)

It's a very common task to work with an array of values, each of the same type. Integers, strings, all kinds of objects etc. But PHP is still a weakly typed language, so it's hard to tell if an arbitrary array actually contains only values of a given type.

Of course, you can always use a class:

```

class IntArray {

private $values = [];

public function add(int $value) {

$this->values[] = $value;

}

}

```

Then, whenever you need an array of integers, you may write something like this:

```

class BatchProcessor

{

private $ids;

public function __construct(IntArray $ids) {

$this->ids = $ids;

}

}

```

Not bad. You'll need a class per type, though, and that may seem a bit of an overkill for such a simple task. Luckily, same result can be achieved differently, but with the same level of confidence in every value type:

```

class BatchProcessor

{

/ / @var int[]

private $ids;

public function __construct(int ...$ids) {

$this->ids = $ids; / / you can be sure that $this->ids is an array of integers!

}

}

```

Voila - no need for extra class! This approach uses the PHP 7's type hinting for scalar types, in conjunction with [Variable length argument lists](http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list) available since PHP 5.6. In fact, variable-length argument lists have been around since probably the very first version of the language, but in 5.6 they were revisited and got some nice syntactic sugar in form of "...", so you declare and call them as easy as this:

```

function func(...$args){...}

$args = [1, 2, 3];

func(...$args);

```

The approach can work with any type-hinting available in PHP, and I hope you find it somewhat useful! Comment, discuss share and ask questions - I'll be around.

1822 views · 2 years ago

![PHP Basics](https://images.ctfassets.net/vzl5fkwyme3u/6mNUFZDod2SQKm20g4YsoC/b36221b3a8260ee3047a377edc6470b1/AdobeStock_69092685.jpeg?w=1000)

It's 2018, and you're a top-notch modern web developer, with a load of knowledge and tools right there at your disposal: Google and StackOverflow, debugger with a GUI, IDE with autocomplete, you name it. Occasionally, though, you still find yourself in a plain old text console on a remote server, or you have to do something without IDE, or there is no network connection... In such cases it might be helpful to feel comfortable in a simple terminal. In this post I'm going to list some switches for the PHP command that you can use to get information and some utilities.

### Getting basic information about PHP

```

$ php -i

phpinfo()

PHP Version => 7.2.10-0ubuntu1

System => Linux awesome 4.18.0-10-generic #11-Ubuntu SMP Thu Oct 11 15:13:55 UTC 2018 x86_64

Build Date => Sep 13 2018 13:38:55

Server API => Command Line Interface

Virtual Directory Support => disabled

...

```

Full information about your PHP interpreter. Pipe the output to the Unix `less` command in order to get pagination and search: `php -i | less`. Type `Q` to exit the `less` shell. Some distros might lack `less`, in that case you may try `php -i | more`, which doesn't give you search but still has pagination.

Want a short summary of which PHP extensions you have? Just ask:

```

$ php -m

[PHP Modules]

calendar

Core

ctype

date

dom

ds

exif

...

```

### More specific info about core and extensions' functions and classes

Full information about functions and classes provided by an extension:

```

$ php --re ds

Extension [ <persistent> extension #46 ds version 1.2.6 ] {

- Dependencies {

Dependency [ json (Required) ]

Dependency [ spl (Required) ]

}

- Classes [11] {

Interface [ <internal:ds> interface Ds\Hashable ] {

- Constants [0] {

}

- Static properties [0] {

}

...

```

Information on a specific class:

```

$ php --rc Ds\\Vector

Class [ <internal:ds> <iterateable> final class Ds\Vector implements Ds\Sequence, Traversable, Countable, JsonSerializable, Ds\Collection ] {

- Constants [1] {

Constant [ public integer MIN_CAPACITY ] { 8 }

}

- Static properties [0] {

}

...

```

Same for a function:

```

$ php --rf fopen

Function [ <internal:standard> function fopen ] {

- Parameters [4] {

Parameter #0 [ <required> $filename ]

Parameter #1 [ <required> $mode ]

Parameter #2 [ <optional> $use_include_path ]

Parameter #3 [ <optional> $context ]

}

}

```

### Utilities

Ever found yourself creating a dummy PHP file of just a few lines - only to be run once and then deleted? The `-a` switch might be what you're looking for:

```

$ php -a

Interactive mode enabled

php > var_dump(join(", ", [1, 2, 3]));

php shell code:1:

string(7) "1, 2, 3"

php >

```

It starts an interactive shell so you can type any PHP code and execute it straight away. Requires PHP to be compiled with `readline` support (most distros have that anyway).

Want a quick check for any parse/syntax errors in a PHP file? Use linter:

```

$ php -l test.php

PHP Parse error: syntax error, unexpected 'array_shift' (T_STRING) in test.php on line 4

Errors parsing test.php

```

### It has a web-server!

Yes! Just in case you missed it, as of PHP 5.4.0, the CLI SAPI provides a built-in web server. Want a quick look at a web page generated by an app? Here you go:

```

$ cd /my_application/document_root

$ php -S localhost:8000

```

Then open http://localhost:8000/ in your browser of choice and enjoy!

Hope you also have enjoyed this reading. Your feedback and questions are always appreciated!

3007 views · 2 years ago

Halloween is filled with ghouls, ghosts, zombies and lots of other spookiness, but the scariest thing ever is FOMO. It’s conference season and several have their call for papers out (including us at Nomad PHP :D). This is the perfect time for you to share your knowledge with the community. Whether it’s your first time or 100th time - it can be scary to put yourself out there and do a talk, but worse than that is not taking the chance and submitting your talk and doing the presentation. Plus, many of these events host lightning talks (short 5-15 minute talks) - meaning you can test out your talk risk free ;)

![](https://i.imgur.com/xByII.jpg)

So here is your chance - submit for one or submit for all of them. May the odds be ever in your favor!

## Fosdem 2019

First we have Fosdem 2019 which will take place on February 2 & 3 in Brussels,Belgium. Some facts about this call for papers:

* **Deadline:** November 3, 2018

* Presentations are expected to be 50 minutes long (including audience questions) and should cater to a varied technical audience. For examples check out [youtube](https://www.youtube.com/watch?v=SdmkLXMwt_g&list=PLaVkMRyQacUQYxXkzvcZJm-kc_FEJpkxK).

* **Submit** your proposals via Pentabarf: [https://fosdem.org/submit](https://fosdem.org/submit).

* The conference covers reasonable travel expenses agreed upon in advance as well as arranges accommodations

## Midwest PHP 2019

Next up we have Midwest PHP which will take place on March 8 & 9 in Bloomington, Minnesota.

* **Deadline:** November 15, 2018

* There is a speaker package included (conference pass, 2 hotel nights, airfare/travel - $500 max, lunch, etc.)

* Make sure the talk title and abstract define the exact topic and what you hope people will learn from it.

* Recommended to submit more than one talk because it can increase your chances of one of them being picked.

* **Submit** your talk here: [https://cfp.midwestphp.org/] (https://cfp.midwestphp.org/)

## Longhorn PHP

Next we have Longhorn PHP which will take place on May 2 (tutorial day) then MAy 3 &4 (conference) in Austin, Texas.

* **Deadline:** December 15, 2018

* For all speakers, you'll get a full conference pass (tutorial day and main conference days), including access to lunch, after-parties, and any other activities included in the conference.

* For speakers remote to the Austin area, we'll provide 3 nights at the speaker hotel (4 nights if presenting a talk and a tutorial) near the conference venue.

* For speakers outside Texas, we'll book you an Economy or equivalent round-trip airfare on a flight into Austin we'd be comfortable taking ourselves (we're conference speakers too!). Plus, we'll arrange transportation between the Austin airport and the speaker hotel.

* Three different session lengths: 3 hour tutorials, 60 minute talks, and 30 minute talks.

* It doesn't have to just be a PHP related talk. For more information on talks click [here](https://cfp.longhornphp.com/ideas).

* **Submit** your talk here: [https://cfp.longhornphp.com/](https://cfp.longhornphp.com/).

## Laravel Live India 2019

Then we have LaravelLive India 2019 in Mumbai, India.

* **Deadline:** December 31, 2018

* Talk length is 30 minutes - Q&A up to the presenters discretion but would be included in the 30 minute time limit.

* Talks will be recorded and distributed for free as well as the presentation slides.

* Looking for a range of talks from PHP (security, testing and frameworks), web development, HTML5, JavaScript, mobile development, emerging technologies and non-technical proposals that will appeal to developers.

* **Talk guidelines:** Objective with clear expectation for audience, short and to the point description, mention of employer is only allowed at the beginning of the content and background image/wallpaper shouldn’t include company name/logos.

* **Submit** your talk here: [https://www.papercall.io/laravellive-india] (https://www.papercall.io/laravellive-india)

## Nomad PHP

#### (you know you want to)

Last but not least - this is an ongoing call for papers. This is perfect if you want to present from the comfort of your office, home or really wherever you are. It’s via RingCentral meetings and will be live and recorded. This is for none other than Nomad PHP.

* **Deadline:** Anytime :D

* Talk length: 45 - 60 minutes.

* Talks should be unique to Nomad PHP and not available in video format online.

* Talk should not be recorded or made available elsewhere online for at least 3 months following your talk.

* The talk will be featured on our page and promoted via social media.

* Speakers will receive a financial stipend.

* Upon being selected we will reach out with further details.

* **Submit** here: [https://www.papercall.io/nomadphp] (https://www.papercall.io/nomadphp)

Now that you have some information - it’s the perfect time to take it all in and get started on your talk proposals :)! Looking forward to seeing all the amazing talks that will be coming out!!!

2544 views · 2 years ago

![Oprah: You Get Extension! Everyone Get Extension!](https://i.imgflip.com/2kug6u.jpg)

Ever wanted to publish your own extension for PHP but stopped by the lack of C language background? Well, maybe it's time for another take. [Zephir language](https://zephir-lang.com/en) is targeted at people like you.

If you follow this link, you will find these words that say a lot about this project:

> Zephir, an open source, high-level language designed to ease the creation and

maintainability of extensions for PHP with a focus on type and memory safety.

Its syntax highly resembles that of PHP, only there's no dollars scattered around your code. Dollar signs, I mean, the PHP `$variables`. You only can create object oriented extensions, and all the classes written in Zephir must be namespaced. A different and stricter type system exists in Zephir, which allows for transpiling the code you write, into a real C extension.

Why would you need it? This question is largely answered here: [Why Zephir](https://docs.zephir-lang.com/en/0.10/motivation). I can only add that, from experience, if you expect a crazy performance boost, you probably won't get it. First, most of the time the bottleneck is not PHP but the IO: database interactions, network and filesystem interactions etc. You won't gain anything by switching to different programming language, in that regards. However, some CPU-intensive calculations may run a lot faster, and if you worry about someone stealing you intellectual property - it also might be worth checking out, because then you can ship your software in form of a binary.

Zephir is the language in which the fastest modern PHP framework is written - [Phalcon](https://phalconphp.com/en/). Whether or not to use it - decide for yourself, it obviously has its pros and cons. But it's certainly interesting to know that you have the ability to approach some of your tasks completely differently!

See you around, and feel free to comment!

4224 views · 2 years ago

![Welcome to PHP 7.1](https://images.ctfassets.net/vzl5fkwyme3u/1anObT98iWWEyA8GeW0ayO/66e8598a6afd88248516cfe347d80d0f/AdobeStock_188454354.jpeg?w=1000)

In case you are living under a rock, the latest version of PHP released last week. PHP developers around the world began rebuilding their development containers with it so they can run their tests. Now it’s your turn. If you haven’t already installed it, you can download it here http://php.net/downloads.php Grab it, get it running in your development environment, and run those unit tests. If all goes well, you can begin planning your staged deployment to production.

If you need a quick start guide to get you going, our good friend Mr Colin O’Dell has just the thing for you [“Installing PHP 7.1”](https://www.colinodell.com/blog/2016-12/installing-php-7-1). It’ll get you up and going quickly on PHP 7.1.

What’s the big deal about PHP 7.1? I am so glad you asked. Here are the major new features released in PHP 7.1.

* [Nullable types](https://wiki.php.net/rfc/nullable_types)

* [Void return type](https://wiki.php.net/rfc/void_return_type)

* [Iterable pseudo-type](https://wiki.php.net/rfc/iterable)

* [Class constant visiblity modifiers](https://wiki.php.net/rfc/class_const_visibility)

* [Square bracket syntax for list()](https://wiki.php.net/rfc/short_list_syntax) and [the ability to specify keys in list()](https://wiki.php.net/rfc/list_keys)

* [Catching multiple exceptions types](https://wiki.php.net/rfc/multiple-catch)

Now if you want a quick intro to several of these new features, check out our [“RFCs of the Future”](https://www.youtube.com/playlist?list=PLm4GlvYCpj51ne45a7Xon0Ao02PEWG7Ti) playlist on YouTube. In it, I talk about 4 of the new features.

Oh and while you are watching things download & compile, why not take the time to give a shoutout to all the core contributors, and a special thank you to [Davey Shafik](https://twitter.com/dshafik) and [Joe Watkins](https://twitter.com/krakjoe), the PHP 7.1 release managers.

Cheers!

=C=

SPONSORS